home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / misc / unix / adt.c next >
C/C++ Source or Header  |  1997-12-11  |  80KB  |  4,150 lines

  1. #ifdef _LILA_ENTE_
  2.  ############################################################################
  3.  ##                                                                        ##
  4.  ## This is ADT 2.4, a user friendly frontend for all Aminet FTP sites. It ##
  5.  ## can be compiled easily on any UNIX flavor and AmigaDOS. Under UNIX,    ##
  6.  ## compile it using the command                                           ##
  7.  ##                                                                        ##
  8.  ##   sh adt.c            (you may append flags, e.g. sh adt.c -DNO_FTP)   ##
  9.  ##                                                                        ##
  10.  ## or use simply type make (your current dir should be where adt.c is)    ##
  11.  ##                                                                        ##
  12.  ## Improvements of adt.c version 2.4:                                     ##
  13.  ## - updated builtin site list                                            ##
  14.  ##                                       ##
  15.  ##         Urban Mueller <umueller@aminet.net>               ##
  16.  ##                                       ##
  17.  ## See adt.c.readme for further notes                                     ##
  18.  ##                                                                        ##
  19.  ############################################################################
  20.  
  21.  libs="-lcurses -ltermcap"
  22.  for i in /usr/lib/libsocket* ; do if [ -r $i ] ; then  # Kludge for systems
  23.   libs="$libs -lsocket -lnsl" ; break                   # with bash as /bin/sh
  24.  fi ; done                                              # like NetBSD or Linux
  25.  
  26.  echo "Compiling... if all compiles OK, type ./adt to start"
  27.  echo ""
  28.  echo cc -O -s -o adt $* $0 $libs
  29.  exec cc -O -s -o adt $* $0 $libs
  30.  exit
  31. #endif
  32.  
  33. #include <stdio.h>
  34.  
  35. #ifdef AMIGA
  36. # define getchr getch
  37. # define makedir(x,y) mkdir(x)
  38. # define popen(x,y) stdout
  39. # define pclose(x)
  40. # define sleep(x)
  41. # define COPYCMD "copy"
  42. # define CURDIR "\"\""
  43. # define TEMPPATH "t:"
  44. # define NO_FTP
  45. # define NO_FIND_CLIENT
  46. # define _system(x) puts(x)
  47. #else
  48. # define getchr getchar
  49. # define makedir(x,y) mkdir(x,y)
  50. /*  # define COPYCMD "cp"            DO NOT USE COPY FOR DOWNLOAD VIA NFS */
  51. # define COPYCMD "ln -s"          /* use softlink instead of, and cp manually */
  52. # define CURDIR "."
  53. # define TEMPPATH "/tmp/"
  54. #endif
  55.  
  56. #ifdef NO_FTP
  57. # define NO_BUILTIN_FTP
  58. # define NO_EXTERNAL_FTP
  59. #endif
  60.  
  61. #ifdef NO_BUILTIN_FTP
  62. # ifdef NO_FIND_CLIENT
  63. #  define NO_TCP_UTILITY
  64. # endif
  65. #else /* !NO_BUILTIN_FTP */
  66. # define NO_EXTERNAL_FTP
  67. #endif
  68.  
  69. /*---------------------------------INCLUDES---------------------------------*/
  70. #define _INCLUDE_POSIX_SOURCE
  71.  
  72. #ifndef AMIGA
  73. # include <sys/types.h>
  74.  
  75. # ifdef DEBUG
  76. #  include <stdlib.h>
  77. #  include <unistd.h>
  78. #  include <string.h>
  79. #  include <sys/param.h>
  80. #  include <sys/stat.h>
  81. # endif
  82.  
  83. # ifndef NO_EXTERNAL_FTP
  84. #  include <sys/stat.h>
  85. # endif
  86.  
  87. # ifndef NO_TCP_UTILITY
  88. #  include <sys/file.h>
  89. #  include <stdio.h>
  90. #  include <netdb.h>
  91. #  include <sys/socket.h>
  92. #  include <netinet/in.h>
  93. #  include <arpa/inet.h>
  94. # endif
  95. #endif
  96.  
  97. #ifndef NO_CURSES
  98. # include <curses.h>
  99. #endif
  100.  
  101. #include <signal.h>
  102. /*---------------------------------GLOBALS-----------------------------------*/
  103.  
  104. #define E_FILE 20
  105. #define E_DIR  10
  106. #define E_DESC 44
  107.  
  108. #define K_CTRL -'@'
  109.  
  110. struct adt_entry {
  111.   long time;
  112.   long size;
  113.   long readmesize;
  114.   short version;
  115.   char readmelen;
  116.   char tagged;
  117.   char osversion;
  118.   char status;
  119.   char file[E_FILE + 1];
  120.   char dir[E_DIR + 1];
  121.   char desc[E_DESC + 1];
  122. };
  123. #define ENTRY struct adt_entry
  124.  
  125. #define STATUS_LOCAL  0
  126. #define STATUS_REMOTE 1
  127.  
  128.  
  129. struct adt_list {
  130.   ENTRY **list;
  131.   char *(*disp) ();
  132.   int mem;
  133.   int num;
  134.   int offset;
  135.   int current;
  136.   char type;
  137.   char top;
  138.   char *sel;
  139.   char *sort;
  140.   char *cmds;
  141. };
  142. #define LIST struct adt_list
  143.  
  144.  
  145. struct adt_transfer {
  146.   char *name;
  147.   char *text;
  148.   int (*init) ();
  149. };
  150. #define TRANSFER struct adt_transfer
  151.  
  152. extern TRANSFER TransferTypes[];
  153.  
  154.  
  155. LIST AllFiles, VisibleFiles, Marked, Alternate;
  156. LIST *File = &AllFiles, *Vis = &VisibleFiles, *Mark = &Marked, *Alt = &Alternate;
  157.  
  158. int Dispmode;
  159. int Width = 80, Height = 25;
  160. int LastCall;
  161. char *NoFilesString = "No files available";
  162. char HomeDir[250];
  163. char UserName[50] = "user";
  164. char ConfigFile[200] = ".adtrc";
  165. char *FindDesc = "Performing local case insensitive substring search";
  166. char GotComplete, GotLocal, GotRecent;
  167. char Flat, Readme, Silent, Path[250], Action;
  168. char InitialSetup;
  169. char Connected;
  170. char MaskSIGINT, GotSIGINT;
  171.  
  172. int (*disp_init) ();
  173. int (*disp_cleanup) ();
  174. int (*disp_choice) ();
  175. int (*disp_mainloop) ();
  176. int (*disp_report) ();
  177. int (*disp_status) ();
  178. int (*disp_confirm) ();
  179. int (*disp_more) ();
  180. int (*disp_getchar) ();
  181. int (*disp_refresh) ();
  182. int (*disp_clear) ();
  183. char *(*disp_inputstr) ();
  184.  
  185. int (*trans_options) ();
  186. int (*trans_connect) ();
  187. int (*trans_remopen) ();
  188. int (*trans_download) ();
  189. int (*trans_disconnect) ();
  190. int (*trans_close) ();
  191.  
  192. /*----------------------------main exit point--------------------------------*/
  193. int exit_adt(err)
  194.   char *err;
  195. {
  196.   if (disp_cleanup)
  197.     disp_cleanup();
  198.  
  199.   if (trans_close)
  200.     trans_close();
  201.  
  202.   if (err)
  203.     puts(err);
  204.  
  205.   exit(err ? 20 : 0);
  206.  
  207.   return 0;
  208. }
  209.  
  210. /*--------------------------quick entry allocator----------------------------*/
  211. #define CHUNKALLOC 100
  212.  
  213. struct EntryChunk {
  214.   struct EntryChunk *next;
  215.   ENTRY chunks[CHUNKALLOC];
  216. };
  217. #define CHUNK struct EntryChunk
  218.  
  219. int InCount;
  220. CHUNK *EntryChunk, *ChunkList;
  221.  
  222. char *salloc(len)
  223.   int len;
  224. {
  225.   char *t;
  226.  
  227.   if (!(t = (char *) malloc(len)))
  228.     exit_adt("out of memory\n");
  229.  
  230.   return t;
  231. }
  232.  
  233.  
  234. ENTRY *e_alloc()
  235. {
  236.   if (!InCount) {
  237.     InCount = CHUNKALLOC;
  238.     EntryChunk = (CHUNK *) salloc(sizeof(CHUNK));
  239.     EntryChunk->next = ChunkList;
  240.     ChunkList = EntryChunk;
  241.   }
  242.   return EntryChunk->chunks + --InCount;
  243. }
  244.  
  245. int e_add(l, e)
  246.   LIST *l;
  247.   ENTRY *e;
  248. {
  249.   ENTRY **oldlist;
  250.   int i;
  251.  
  252.   if (!l->mem) {
  253.     l->mem = 100;
  254.  
  255.     l->list = (ENTRY **) salloc(l->mem * sizeof(ENTRY *));
  256.   }
  257.   if (l->num == l->mem) {
  258.     oldlist = l->list;
  259.  
  260.     l->list = (ENTRY **) salloc(2 * l->mem * sizeof(ENTRY *));
  261.  
  262.     for (i = 0; i < l->num; i++)
  263.       l->list[i] = oldlist[i];
  264.  
  265.     l->mem *= 2;
  266.     free(oldlist);
  267.   }
  268.   l->list[l->num++] = e;
  269.   return 0;
  270. }
  271.  
  272. int l_reset(l)
  273.   LIST *l;
  274. {
  275.   if (l->list)
  276.     free(l->list);
  277.   l->list = 0;
  278.   l->mem = 0;
  279.   l->num = 0;
  280.   l->current = 0;
  281.   return 0;
  282. }
  283.  
  284. int l_store()
  285. {
  286.   int i;
  287.   ENTRY **e1, **e2;
  288.  
  289.   if (Alt->list)
  290.     free(Alt->list);
  291.  
  292.   if (Vis->mem)
  293.     Alt->list = (ENTRY **) salloc(Vis->mem * sizeof(ENTRY *));
  294.  
  295.   for (i = Vis->num - 1, e1 = Vis->list, e2 = Alt->list; i >= 0; i--)
  296.     *e2++ = *e1++;
  297.  
  298.   Alt->list = Vis->list;
  299.   Alt->num = Vis->num;
  300.   Alt->mem = Vis->mem;
  301.   Alt->offset = Vis->offset;
  302.   Alt->current = Vis->current;
  303.   Alt->sel = Vis->sel;
  304.   Alt->sort = Vis->sort;
  305.  
  306.   return 0;
  307. }
  308.  
  309. int e_freeall()
  310. {
  311.   CHUNK *c, *next;
  312.  
  313.   l_reset(Alt);
  314.   l_reset(Mark);
  315.   l_reset(Vis);
  316.   l_reset(File);
  317.  
  318.   for (c = ChunkList; c; c = next) {
  319.     next = c->next;
  320.     free(c);
  321.   }
  322.   ChunkList = 0;
  323.   InCount = 0;
  324.   GotComplete = GotLocal = GotRecent = 0;
  325.  
  326.   return 0;
  327. }
  328.  
  329. /*-------------------------string funcs for portability----------------------*/
  330. int mystrlen(s)
  331.   char *s;
  332. {
  333.   int n = 0;
  334.  
  335.   while (*s++)
  336.     n++;
  337.   return n;
  338. }
  339.  
  340. int mystrncpy(s1, s2, n)
  341.   char *s1, *s2;
  342.   int n;
  343. {
  344.   while (*s2 && n--)
  345.     *s1++ = *s2++;
  346.  
  347.   *s1++ = 0;
  348.  
  349.   return 0;
  350. }
  351.  
  352. char *mystrcpy(s1, s2)
  353.   char *s1, *s2;
  354. {
  355.   char *t = s1;
  356.  
  357.   while ((*s1++ = *s2++));
  358.  
  359.   return t;
  360. }
  361.  
  362. int mystrcmp(s1, s2)
  363.   char *s1, *s2;
  364. {
  365.   while (*s1 == *s2 && *s1)
  366.     s1++, s2++;
  367.  
  368.   return *s1 - *s2;
  369. }
  370.  
  371. #define LOWER(x) (x<='Z' && x>='A' ? x-'A'+'a' : x)
  372.  
  373. int mystricmp(s1, s2)
  374.   char *s1, *s2;
  375. {
  376.   while (LOWER(*s1) == LOWER(*s2) && *s1)
  377.     s1++, s2++;
  378.  
  379.   return LOWER(*s1) - LOWER(*s2);
  380. }
  381.  
  382. int mystrncmp(s1, s2, n)
  383.   char *s1, *s2;
  384.   int n;
  385. {
  386.   while (n && *s1 && *s1 == *s2)
  387.     s1++, s2++, n--;
  388.  
  389.   return n != 0 && *s1 - *s2;
  390. }
  391.  
  392. int mystrnicmp(s1, s2, n)
  393.   char *s1, *s2;
  394.   int n;
  395. {
  396.   while (n && *s1 && LOWER(*s1) == LOWER(*s2))
  397.     s1++, s2++, n--;
  398.  
  399.   return n != 0 && LOWER(*s1) - LOWER(*s2);
  400. }
  401.  
  402. char *myfgets(buf, len, file)
  403.   char *buf;
  404.   int len;
  405.   FILE *file;
  406. {
  407.   char *t, *r;
  408.  
  409.   if ((r = fgets(buf, len, file))) {
  410.     for (t = buf; *t && *t != '\n'; t++);
  411.  
  412.     *t = 0;
  413.   }
  414.   return r;
  415. }
  416.  
  417. char *mystristr(str1, str2)
  418.   char *str1, *str2;
  419. {
  420.   char c = LOWER(*str1), l = strlen(str1);
  421.  
  422.   for (;;) {
  423.     while (*str2 && LOWER(*str2) != c)
  424.       str2++;
  425.  
  426.     if (!*str2)
  427.       break;
  428.  
  429.     if (!mystrnicmp(str1, str2, l))
  430.       return str2;
  431.  
  432.     str2++;
  433.   }
  434.  
  435.   return 0;
  436. }
  437.  
  438. long myatoi(s)
  439.   char *s;
  440. {
  441.   long r = 0;
  442.  
  443.   while (*s && *s >= '0' && *s <= '9')
  444.     r = 10 * r + *s++ - '0';
  445.  
  446.   return r;
  447. }
  448.  
  449. char rdmbuf[200];
  450.  
  451. char *
  452. get_readme(e)
  453.   ENTRY *e;
  454. {
  455.   strcpy(rdmbuf,e->file);
  456.   strcpy(rdmbuf+strlen(rdmbuf)-e->readmelen, ".readme");
  457.   return rdmbuf;
  458. }
  459.  
  460. #define MYMIN(x,y) ((x)<(y) ? (x) : (y))
  461. #define MYMAX(x,y) ((x)>(y) ? (x) : (y))
  462.  
  463.  
  464. #ifdef AMIGA
  465.  
  466. char *glob_path (buf)
  467.   char *buf;
  468. {
  469.   return buf;
  470. }
  471.  
  472. #else
  473.  
  474. #include <pwd.h>
  475.  
  476. char *glob_path (buf)
  477.   char *buf;
  478. {
  479.   short bindex = 1;
  480.   struct passwd pw_info, *pw;
  481.   char buf2[300];
  482.  
  483.   if (buf[0] != '~')
  484.     return buf;
  485.  
  486.   pw_info.pw_dir = (char *) getenv("HOME");
  487.   if (buf[1] && buf[1] != '/') {
  488.     char username[128];
  489.  
  490.     while (buf[bindex] && buf[bindex] != '/')
  491.         ++bindex;
  492.     memcpy( username, buf+1, bindex-1);
  493.     username[bindex-1] = 0;
  494.  
  495.     if (pw = getpwnam(username))
  496.       pw_info.pw_dir = pw->pw_dir;
  497.     else
  498.       pw_info.pw_dir = "~";
  499.   }
  500.  
  501.   strcpy (buf2, pw_info.pw_dir);
  502.   strcat (buf2, buf + bindex);
  503.   strcpy (buf, buf2);
  504.  
  505.   return buf;
  506. }
  507. #endif
  508.  
  509.  
  510. /*-----------------------------options and settings--------------------------*/
  511. char *Options[] = {
  512.   "method", "ftp",
  513.   "fspsite", "epix.rrze.uni-erlangen.de,21",
  514.   "fspsites", "epix.rrze.uni-erlangen.de,21",
  515.   "mailserv", "ftp.doc.ic.ac.uk",
  516.   "timeout", "30",
  517.   "nomotd", "0",
  518.   "amotd", "0",
  519.   "lmotd", "0",
  520.   "sites", "0",
  521.   "locaminet", "/public/pub/aminet",
  522.   "unpack", "",
  523.   "transfer", "",
  524.   "newest", "0",
  525.   "hide", "",
  526.   "printer", "",
  527.   "dlpath", "",
  528.   "compress", "1",
  529.   "send", "sz",
  530.   "silentdl", "0",
  531.   "readmedl", "0",
  532.   "flatdl", "1",
  533.   "ftpsite", "ftp.eunet.ch",
  534.   "ftpmaster", "ftp.wustl.edu",
  535.   "ftpsites","\
  536. USA,us.aminet.net,128.252.135.4,,@\
  537. Germany:Paderborn,de.aminet.net,131.234.22.34,,@\
  538. UK,uk.aminet.net,193.63.255.4,,@\
  539. Italy,it.aminet.net,192.132.34.17,,@\
  540. Sweden,se.aminet.net,130.238.253.4,,@\
  541. Australia,au.aminet.net,203.16.26.3,,@\
  542. Germany:Berlin,de2.aminet.net,130.149.17.12,,de.aminet.net@\
  543. Germany:Erlangen,de3.aminet.net,131.188.3.71,,de.aminet.net@\
  544. Germany:Trier,de4.aminet.net,136.199.8.81,,de.aminet.net@\
  545. Germany:Bremen,de5.aminet.net,134.102.228.2,,de.aminet.net@\
  546. Germany:Stuttgart,de6.aminet.net,129.69.18.15,,de.aminet.net@\
  547. Germany:Regensburg,de7.aminet.net,132.199.2.11,,de.aminet.net@\
  548. Germany:K'lautern,de8.aminet.net,131.246.94.94,,de.aminet.net@\
  549. Germany:Augsburg,de9.aminet.net,141.82.16.242,,de.aminet.net@\
  550. Germany:Dortmund,de10.aminet.net,129.217.128.54,,de.aminet.net@\
  551. Germany:Aachen,de11.aminet.net,137.226.225.3,,de.aminet.net@\
  552. Germany:Clausthal,de12.aminet.net,139.174.253.13,,de.aminet.net@\
  553. Austria,at.aminet.net,131.130.186.194,,it.aminet.net@\
  554. France,fr.aminet.net,194.158.96.22,,it.aminet.net@\
  555. Sweden,se2.aminet.net,130.240.16.39,,se.aminet.net@\
  556. Croatia,hr.aminet.net,161.53.129.16,,it.aminet.net@\
  557. Switzerland,ch.aminet.net,146.228.10.11,,it.aminet.net@\
  558. Denmark,dk.aminet.net,130.225.51.30,,se.aminet.net@\
  559. Ireland,ie.aminet.net,143.239.1.200,,uk.aminet.net@\
  560. Spain,es.aminet.net,157.88.36.190,,it.aminet.net@\
  561. Poland,pl1.aminet.net,194.92.39.69,,uk.aminet.net@\
  562. Poland,pl2.aminet.net,195.117.124.1,,uk.aminet.net",
  563.   "findmethod", "0",
  564.   "findsite", "",
  565.   "findsites", "\
  566. USA (MO),us.aminet.net,128.252.135.4,,@\
  567. Germany,de.aminet.net,131.234.22.34,,@\
  568. UK,uk.aminet.net,193.63.255.4,,@\
  569. Italy,it.aminet.net,192.132.34.17,,@\
  570. Australia,au.aminet.net,203.16.26.3,,",
  571.   0, 0
  572. };
  573.  
  574. #define OBUFLEN 8192
  575. char obuf[OBUFLEN];
  576.  
  577. char *option_get(name)
  578.   char *name;
  579. {
  580.   char **t;
  581.  
  582.   for (t = Options; t[0]; t += 2)
  583.     if (!mystrcmp(name, t[0]))
  584.       return t[1];
  585.  
  586.   return "";
  587. }
  588.  
  589. int option_getnum(name)
  590.   char *name;
  591. {
  592.   return myatoi(option_get(name));
  593. }
  594.  
  595. int option_set(name, val)
  596.   char *name, *val;
  597. {
  598.   char **t;
  599.  
  600.   for (t = Options; t[0]; t += 2)
  601.     if (!mystrcmp(name, t[0]))
  602.       mystrcpy((t[1] = salloc(strlen(val) + 1)), val);
  603.  
  604.   return 0;
  605. }
  606.  
  607. int option_setnum(name, val)
  608.   char *name;
  609.   int val;
  610. {
  611.   char buf[20];
  612.  
  613.   sprintf(buf, "%d", val);
  614.   option_set(name, buf);
  615.  
  616.   return 0;
  617. }
  618.  
  619. int option_loadfh(f)
  620.   FILE *f;
  621. {
  622.   char *t;
  623.  
  624.   fgets(obuf, OBUFLEN, f);
  625.   if (mystrncmp("#adtrc-v2", obuf, 9))
  626.     return 1;
  627.  
  628.   while (myfgets(obuf, OBUFLEN, f)) {
  629.     while (*obuf && obuf[strlen(obuf)-1]=='\\' && 
  630.            myfgets(obuf+strlen(obuf)-1, OBUFLEN-strlen(obuf)-1, f)) ;
  631.       
  632.     for (t = obuf; *t && *t != '='; t++);
  633.  
  634.     if (*t == '=') {
  635.       *t++ = 0;
  636.       option_set(obuf, t);
  637.     }
  638.   }
  639.  
  640.   return 0;
  641. }
  642.  
  643. int option_load(name)
  644.   char *name;
  645. {
  646.   FILE *f;
  647.   int r;
  648.  
  649.   if (!(f = fopen(name, "r")))
  650.     return 1;
  651.  
  652.   r = option_loadfh(f);
  653.  
  654.   fclose(f);
  655.  
  656.   return r;
  657. }
  658.  
  659. int option_save(name)
  660.   char *name;
  661. {
  662.   FILE *f;
  663.   char **t;
  664.  
  665.   if (!(f = fopen(name, "w")))
  666.     return 1;
  667.  
  668.   fprintf(f, "#adtrc-v2\n");
  669.  
  670.   for (t = Options; t[0]; t += 2)
  671.     fprintf(f, "%s=%s\n", t[0], t[1]);
  672.  
  673.   fclose(f);
  674.  
  675.   return 0;
  676. }
  677.  
  678. /*-----------------------------input file parser-----------------------------*/
  679. #define PBUFSIZE 400
  680. char parsebuf[PBUFSIZE], p_error, *p_ptr;
  681.  
  682. int reset_token()
  683. {
  684.   p_ptr = parsebuf;
  685.   p_error = 0;
  686.   return 0;
  687. }
  688.  
  689.  
  690. char *get_token()
  691. {
  692.   char *t, *s, *d;
  693.  
  694.   if (!p_ptr) {
  695.     p_error = 1;
  696.     return "";
  697.   }
  698.   for (t = s = d = p_ptr; *s && *s != '@'; s++) {
  699.     if (*s != '\r')
  700.       *d++ = *s;
  701.   }
  702.  
  703.   if (!*s)
  704.     p_ptr = 0;
  705.   else {
  706.     *s = 0;
  707.     p_ptr = s + 1;
  708.   }
  709.  
  710.   return t;
  711. }
  712.  
  713.  
  714. long p_atoi(s)
  715.   char *s;
  716. {
  717.   long r = 0;
  718.  
  719.   while (*s && *s >= '0' && *s <= '9')
  720.     r = 10 * r + *s++ - '0';
  721.  
  722.   if (*s)
  723.     p_error = 1;
  724.  
  725.   return r;
  726. }
  727.  
  728.  
  729. int read_adt_v2(fh)
  730.   FILE *fh;
  731. {
  732.   ENTRY *e;
  733.   char *t;
  734.  
  735.   do {
  736.     if (*parsebuf == '#')
  737.       if (!mystrncmp(parsebuf, "#endadt", 7))
  738.     break;
  739.       else
  740.     continue;
  741.  
  742.     e = e_alloc();
  743.  
  744.     reset_token();
  745.  
  746.     e->time = p_atoi(get_token());
  747.  
  748.     mystrncpy(e->dir, get_token(), E_DIR);
  749.  
  750.     mystrncpy(e->file, get_token(), E_FILE);
  751.  
  752.     e->size = p_atoi(get_token());
  753.  
  754.     e->readmelen = p_atoi(get_token());
  755.  
  756.     e->readmesize = p_atoi(get_token());
  757.  
  758.     e->version = -1;
  759.  
  760.     e->osversion = -1;
  761.  
  762.     e->status = *get_token()=='R' ? STATUS_REMOTE : STATUS_LOCAL;
  763.  
  764.     mystrncpy(e->desc, get_token(), E_DESC);
  765.  
  766.     for (t = e->desc; *t; t++)
  767.       if (*t == '\n')
  768.     *t = 0;
  769.  
  770.     e->tagged = 0;
  771.  
  772.     if (!(p_error))
  773.       e_add(File, e);
  774.   } while (fgets(parsebuf, PBUFSIZE, fh));
  775.  
  776.   return 0;
  777. }
  778.  
  779. /*----------------------------------utility----------------------------------*/
  780. long newesttime()
  781. {
  782.   int i;
  783.   long newest = 0;
  784.  
  785.   for (i = 0; i < File->num; i++)
  786.     if ((File->list[i]->time < time(NULL)) && (File->list[i]->time > newest))
  787.       newest = File->list[i]->time;
  788.  
  789.   return newest;
  790. }
  791.  
  792. char *temp_name(buf, sub)
  793.   char *buf;
  794.   char *sub;
  795. {
  796.   sprintf(buf, "%s%s_%s", TEMPPATH, UserName, sub);
  797.   return buf;
  798. }
  799.  
  800. struct temp_file {
  801.   FILE *fh;
  802.   char name[100];
  803.   int (*cleanup) ();
  804. };
  805. #define TEMPFILE struct temp_file
  806.  
  807. int temp_delete(tfh)
  808.   TEMPFILE *tfh;
  809. {
  810.   if (*tfh->name)
  811.     unlink(tfh->name);
  812.   return 0;
  813. }
  814.  
  815. int temp_dummy()
  816. {
  817.   return 0;
  818. }
  819.  
  820. char tclose(tfh)
  821.   TEMPFILE *tfh;
  822. {
  823.   fclose(tfh->fh);
  824.   if (tfh->cleanup)
  825.     (*tfh->cleanup) (tfh);
  826.   return 0;
  827. }
  828.  
  829. int exists(name)
  830.   char *name;
  831. {
  832.   FILE *f;
  833.  
  834.   if ((f = fopen(name, "r")))
  835.     fclose(f);
  836.   return f != 0;
  837. }
  838.  
  839. char *tackon(buf, add)
  840.   char *buf, *add;
  841. {
  842.   if (*buf && (buf[strlen(buf) - 1] != '/' && buf[strlen(buf) - 1] != ':'))
  843.     strcat(buf, "/");
  844.   strcat(buf, add);
  845.  
  846.   return buf;
  847. }
  848.  
  849. char *basename(s)
  850.   char *s;
  851. {
  852.   char *b = s;
  853.  
  854.   for (; *s; s++)
  855.     if (*s == '/' || *s == ':')
  856.       b = s + 1;
  857.  
  858.   return b;
  859. }
  860.  
  861.  
  862. #define NUMLINES 500
  863. char *Lines[NUMLINES + 1];
  864. char LineBuf[200];
  865.  
  866. char **read_file(f, title)
  867.   FILE *f;
  868.   char *title;
  869. {
  870.   int i=0;
  871.   char *t;
  872.  
  873.   if (title) {
  874.     Lines[i]= (char *) salloc( mystrlen(title) + 1 );
  875.     mystrcpy (Lines[i++], title);
  876.   }
  877.  
  878.   for ( ; i < NUMLINES; i++) {
  879.     if (!(fgets(LineBuf, 200, f)))
  880.       break;
  881.     for (t = LineBuf; *t && *t != '\n'; t++);
  882.     *t = 0;
  883.     Lines[i] = (char *) salloc(mystrlen(LineBuf) + 1);
  884.     mystrcpy(Lines[i], LineBuf);
  885.   }
  886.   Lines[i] = 0;
  887.   return Lines;
  888. }
  889.  
  890. int free_file(strings)
  891.   char **strings;
  892. {
  893.   while (*strings)
  894.     free(*strings++);
  895.   return 0;
  896. }
  897.  
  898. /*-----------------------------------sorting---------------------------------*/
  899.  
  900. int cmp_age(e1, e2)
  901.   ENTRY *e1, *e2;
  902. {
  903.   return e2->time - e1->time;
  904. }
  905.  
  906. int cmp_dir(e1, e2)
  907.   ENTRY *e1, *e2;
  908. {
  909.   int t;
  910.  
  911.   return (t = mystricmp(e1->dir, e2->dir)) ? t : mystricmp(e1->file, e2->file);
  912. }
  913.  
  914. int cmp_size(e1, e2)
  915.   ENTRY *e1, *e2;
  916. {
  917.   return e2->size - e1->size;
  918. }
  919.  
  920. int cmp_name(e1, e2)
  921.   ENTRY *e1, *e2;
  922. {
  923.   return mystricmp(e1->file, e2->file);
  924. }
  925.  
  926. int cmp_rage(e2, e1)
  927.   ENTRY *e1, *e2;
  928. {
  929.   return e2->time - e1->time;
  930. }
  931.  
  932. int cmp_rdir(e2, e1)
  933.   ENTRY *e1, *e2;
  934. {
  935.   int t;
  936.  
  937.   return (t = mystricmp(e1->dir, e2->dir)) ? t : mystricmp(e1->file, e2->file);
  938. }
  939.  
  940. int cmp_rsize(e2, e1)
  941.   ENTRY *e1, *e2;
  942. {
  943.   return e2->size - e1->size;
  944. }
  945.  
  946. int cmp_rname(e2, e1)
  947.   ENTRY *e1, *e2;
  948. {
  949.   return mystricmp(e1->file, e2->file);
  950. }
  951.  
  952. struct sort_mode {
  953.   int (*cmp) ();
  954.   char *desc;
  955. };
  956. #define SORT struct sort_mode
  957.  
  958. SORT
  959. sort_age   = { cmp_age,   "by age (newest first)" },
  960. sort_dir   = { cmp_dir,   "by dir" },
  961. sort_size  = { cmp_size,  "by size" },
  962. sort_name  = { cmp_name,  "by name" },
  963. sort_rage  = { cmp_rage,  "by age (oldest first)" },
  964. sort_rdir  = { cmp_rdir,  "reverse by dir" },
  965. sort_rsize = { cmp_rsize, "reverse by size" },
  966. sort_rname = { cmp_rname, "reverse by name" };
  967.  
  968. int quick_sort(av, n, cmp)
  969.   ENTRY **av;
  970.   int n, (*cmp) ();
  971. {
  972.   ENTRY **i, **j, *x, *t;
  973.  
  974.   if (n > 0) {
  975.     i = av;
  976.     j = av + n - 1;
  977.     x = av[n >> 1];
  978.     do {
  979.       while (cmp(*i, x) < 0)
  980.     i++;
  981.       while (cmp(x, *j) < 0)
  982.     --j;
  983.       if (i <= j)
  984.     t = *i, *i = *j, *j = t, i++, j--;
  985.  
  986.     } while (i <= j);
  987.  
  988.     if (j + 1 - av < av + n - i) {
  989.       quick_sort(av, j + 1 - av, cmp);
  990.       quick_sort(i, av + n - i, cmp);
  991.     } else {
  992.       quick_sort(i, av + n - i, cmp);
  993.       quick_sort(av, j + 1 - av, cmp);
  994.     }
  995.   }
  996.   return 0;
  997. }
  998.  
  999. int sort_list(list, sort)
  1000.   LIST *list;
  1001.   SORT *sort;
  1002. {
  1003.  
  1004.   quick_sort(list->list, list->num, sort->cmp);
  1005.  
  1006.   list->current = list->offset = 0;
  1007.  
  1008.   Vis->sort = sort->desc;
  1009.  
  1010.   return 0;
  1011. }
  1012.  
  1013.  
  1014. /*---------------------------------visibility--------------------------------*/
  1015.  
  1016. int allvisible()
  1017. {
  1018.   int i;
  1019.  
  1020.   l_store();
  1021.   l_reset(Vis);
  1022.  
  1023.   Vis->sel = "All files";
  1024.   for (i = 0; i < File->num; i++)
  1025.     e_add(Vis, File->list[i]);
  1026.  
  1027.   sort_list(Vis, &sort_dir);
  1028.  
  1029.   return 0;
  1030. }
  1031.  
  1032. int invisible()
  1033. {
  1034.   ENTRY **entr = Vis->list;
  1035.   char buf[200], *b, *h = option_get("hide");
  1036.   int i, j;
  1037.  
  1038.   while (*h) {
  1039.     b = buf;
  1040.     while (*h && *h != ' ' && *h != ',')
  1041.       *b++ = *h++;
  1042.     *b = 0;
  1043.  
  1044.     while (*h && (*h == ' ' || *h == ','))
  1045.       h++;
  1046.  
  1047.     if (!*buf)
  1048.       continue;
  1049.  
  1050.     for (i = j = 0; i < Vis->num; i++)
  1051.       if (mystrncmp(buf, entr[i]->dir, b - buf))
  1052.     entr[j++] = entr[i];
  1053.     Vis->num = j;
  1054.   }
  1055.   Vis->current = 0;
  1056.  
  1057.   return 0;
  1058. }
  1059.  
  1060. int newvisible()
  1061. {
  1062.   ENTRY **entr = File->list;
  1063.   int i;
  1064.  
  1065.   l_store();
  1066.   l_reset(Vis);
  1067.   Vis->sel = "New files";
  1068.  
  1069.   for (i = 0; i < File->num; i++)
  1070.     if (entr[i]->time > LastCall)
  1071.       e_add(Vis, entr[i]);
  1072.  
  1073.   invisible();
  1074.  
  1075.   if (!Vis->num && File->num)
  1076.     NoFilesString = "No new files since your last call, use v)iew k)nown to see older ones";
  1077.  
  1078.   sort_list(Vis, &sort_dir);
  1079.  
  1080.   return 0;
  1081. }
  1082.  
  1083. int dirvisible(dir)
  1084.   char *dir;
  1085. {
  1086.   ENTRY **entr = File->list;
  1087.   int i, l = strlen(dir);
  1088.  
  1089.   l_store();
  1090.   l_reset(Vis);
  1091.   Vis->sel = "Selected dir";
  1092.  
  1093.   for (i = 0; i < File->num; i++)
  1094.     if (!mystrncmp(entr[i]->dir, dir, l))
  1095.       e_add(Vis, entr[i]);
  1096.  
  1097.   if (!Vis->num && File->num)
  1098.     NoFilesString = "No files matching that directory specification";
  1099.  
  1100.   sort_list(Vis, &sort_dir);
  1101.  
  1102.   return 0;
  1103. }
  1104.  
  1105. int findstr(str)
  1106.   char *str;
  1107. {
  1108.   ENTRY **entr = File->list;
  1109.   int i;
  1110.  
  1111.   if (!*str)
  1112.     return 0;
  1113.  
  1114.   l_store();
  1115.   l_reset(Vis);
  1116.  
  1117.   for (i = 0; i < File->num; i++)
  1118.     if (mystristr(str, entr[i]->desc) || mystristr(str, entr[i]->file))
  1119.       e_add(Vis, entr[i]);
  1120.  
  1121.   Vis->sel = "Matching files";
  1122.  
  1123.   sort_list(Vis, &sort_dir);
  1124.  
  1125.   if (!Vis->num && File->num)
  1126.     NoFilesString = "No files matching substring, use f)ind to search again";
  1127.  
  1128.   return 0;
  1129. }
  1130.  
  1131.  
  1132. int strlimit(str)
  1133.   char *str;
  1134. {
  1135.   ENTRY **entr = Vis->list;
  1136.   int i, j;
  1137.  
  1138.   if (!*str)
  1139.     return 0;
  1140.  
  1141.   l_store();
  1142.   Vis->sel = "Matching files";
  1143.  
  1144.   for (i = j = 0; i < Vis->num; i++)
  1145.     if (mystristr(str, entr[i]->desc) || mystristr(str, entr[i]->file))
  1146.       entr[j++] = entr[i];
  1147.  
  1148.   Vis->num = j;
  1149.   Vis->current = 0;
  1150.  
  1151.   if (!Vis->num && File->num)
  1152.     NoFilesString = "No files matching substring, use v)iew to see more";
  1153.  
  1154.   return 0;
  1155. }
  1156.  
  1157. int togglevisible()
  1158. {
  1159.   LIST *l;
  1160.  
  1161.   l = Vis;
  1162.   Vis = Alt;
  1163.   Alt = l;
  1164.   return 0;
  1165. }
  1166.  
  1167.  
  1168. int markedvisible()
  1169. {
  1170.   ENTRY **entr;
  1171.   int i, j=0;
  1172.  
  1173.   l_store();
  1174.   Vis->sel = "Marked files";
  1175.  
  1176.   entr = Vis->list;
  1177.   for (i = 0; i < Vis->num; i++)
  1178.     if (entr[i]->tagged)
  1179.       entr[j++]=entr[i];
  1180.  
  1181.   if (!(Vis->num=j) && File->num)
  1182.     NoFilesString = "No tagged files";
  1183.  
  1184.   return 0;
  1185. }
  1186.  
  1187. int markedextract()
  1188. {
  1189.   ENTRY **entr = Vis->list;
  1190.   int i;
  1191.  
  1192.   l_reset(Mark);
  1193.  
  1194.   for (i = 0; i < Vis->num; i++)
  1195.     if (entr[i]->tagged)
  1196.       e_add(Mark, entr[i]);
  1197.  
  1198.   if (!Mark->num)
  1199.     e_add(Mark, entr[Vis->current]);
  1200.  
  1201.   return 0;
  1202. }
  1203.  
  1204.  
  1205.  
  1206. /*-----------------------------string formatting-----------------------------*/
  1207. char *ShowFiles = "New files by dir";
  1208. int CurSelected, Window;
  1209.  
  1210. char *str_topline(buf, len, list)
  1211.   LIST *list;
  1212.   int len;
  1213.   char *buf;
  1214. {
  1215.   int i;
  1216.   char buf2[40];
  1217.  
  1218.   for (i = 0; i < len; i++)
  1219.     buf[i] = ' ';
  1220.   buf[i] = 0;
  1221.  
  1222.   sprintf(buf2, "Showing: %d/%d", list->num, File->num);
  1223.   strcpy(buf, buf2);
  1224.   buf[strlen(buf)] = ' ';
  1225.  
  1226.   sprintf(buf2, "%s %s", Vis->sel, Vis->sort);
  1227.   strcpy(buf + len / 2 - strlen(buf2) / 2, buf2);
  1228.   buf[strlen(buf)] = ' ';
  1229.  
  1230.   sprintf(buf2, "Page: %d/%d", 1 + list->offset / Window, 1 + list->num / Window);
  1231.   strcpy(buf + len - strlen(buf2), buf2);
  1232.  
  1233.   return buf;
  1234. }
  1235.  
  1236.  
  1237. char sizestr[10];
  1238.  
  1239. char *str_ksize(size)
  1240.   long size;
  1241. {
  1242.   if (size == -1) {
  1243.     sprintf(sizestr, "?");
  1244.   } else {
  1245.     size = (size + 512) / 1024;
  1246.     if (size <= 999)
  1247.       sprintf(sizestr, "%dK", size);
  1248.     else if ((size + 512) / 1024 < 10)
  1249.       sprintf(sizestr, "%d.%dM", size / 1024, (size * 10 + 512) / 1024 % 10);
  1250.     else
  1251.       sprintf(sizestr, "%dM", (size + 512) / 1024);
  1252.   }
  1253.   return sizestr;
  1254. }
  1255.  
  1256.  
  1257. char *str_titles(buf)
  1258.   char *buf;
  1259. {
  1260.   switch (Dispmode) {
  1261.   case 0:
  1262.     strcpy(buf, " File                 Dir        Size Description\n");
  1263.     break;
  1264.   }
  1265.   return buf;
  1266. }
  1267.  
  1268.  
  1269.  
  1270. char *str_entry(e, n, max, buf, len)
  1271.   ENTRY **e;
  1272.   int n, max, len;
  1273.   char *buf;
  1274. {
  1275.   int i;
  1276.  
  1277.   if (n >= max) {
  1278.     for (i = 0; i < len; i++)
  1279.       buf[i] = ' ';
  1280.     buf[i] = 0;
  1281.     return buf;
  1282.   }
  1283.   switch (Dispmode) {
  1284.   case 0:
  1285.     sprintf(buf, "%c%-20.20s %-10.10s %4.4s%c%-80.80s",
  1286.         e[n]->tagged ? '+' : ' ',
  1287.         e[n]->file,
  1288.         e[n]->dir,
  1289.         str_ksize(e[n]->size),
  1290.         e[n]->readmesize > 100 ? '+' : ' ',
  1291.         e[n]->desc);
  1292.     buf[len - 1] = 0;
  1293.     break;
  1294.   }
  1295.  
  1296.  
  1297.   return buf;
  1298. }
  1299.  
  1300.  
  1301. char sitebuf[120];
  1302.  
  1303. int str_sitesplit(s, parts)
  1304.   char *s, **parts;
  1305. {
  1306.   char *d = sitebuf;
  1307.   int i;
  1308.  
  1309.   for (i = 0; i < 5; i++) {
  1310.     parts[i] = d;
  1311.     while (*s && *s != ',' && *s != '@')
  1312.       *d++ = *s++;
  1313.     if (*s == ',')
  1314.       s++;
  1315.     *d++ = 0;
  1316.   }
  1317.  
  1318.   return 0;
  1319. }
  1320.  
  1321. char *str_showline(entries, n, max, buf)
  1322.   char *entries, *buf;
  1323.   int n, max;
  1324. {
  1325.   char *s = entries;
  1326.   char *parts[10];
  1327.  
  1328.   if (n >= max)
  1329.     return "";
  1330.  
  1331.   for (; n > 0; n--)
  1332.     while (*s++ != '@');
  1333.  
  1334.   str_sitesplit(s, parts);
  1335.   sprintf(buf, "%-12.12s %24.24s", parts[0], parts[1]);
  1336.  
  1337.   return buf;
  1338. }
  1339.  
  1340. char centerbuf[250];
  1341.  
  1342. char *str_center(s, len)
  1343.   char *s;
  1344.   int len;
  1345. {
  1346.   int i = (len - strlen(s)) / 2 - 1;
  1347.  
  1348.   strcpy(centerbuf + i, s);
  1349.   while (i > 0)
  1350.     centerbuf[--i] = ' ';
  1351.  
  1352.   return centerbuf;
  1353. }
  1354.  
  1355.  
  1356. int str_longest(arr)
  1357.   char **arr;
  1358. {
  1359.   int l = 0;
  1360.  
  1361.   for (; *arr; arr++)
  1362.     if (strlen(*arr) > l)
  1363.       l = strlen(*arr);
  1364.   return l;
  1365. }
  1366.  
  1367. int str_shift(len, width)
  1368.   int len, width;
  1369. {
  1370.   return len < width ? (width - len) / 2 : 0;
  1371. }
  1372.  
  1373. LIST sitelist = {0, str_showline, 0, 0, 0, 0, 1, 0};
  1374.  
  1375. int setup_sitelist(sitesname, sitename, help)
  1376.   char *sitesname, *sitename, **help;
  1377. {
  1378.   int t, n = 1;
  1379.   char *sites = option_get(sitesname), *s;
  1380.   char buf[100], *d = buf;
  1381.  
  1382.   mystrcpy(buf, option_get("ftpsite"));
  1383.  
  1384.   for (s = sites; *s; ) {
  1385.     if (*s++ == '@') {
  1386.       if (*buf && !mystrncmp(s, buf, strlen(buf)))
  1387.     sitelist.current = n;
  1388.       n++;
  1389.     }
  1390.   }
  1391.  
  1392.   sitelist.list = (ENTRY **) sites;
  1393.   sitelist.num = n;
  1394.  
  1395.   if ((t = disp_choice(help, &sitelist)) < 0)
  1396.     return 1;
  1397.  
  1398.   for (s = sites; t > 0; t--)
  1399.     while (*s++ != '@');
  1400.  
  1401.   while (*s && *s != '@')
  1402.     *d++ = *s++;
  1403.   *d++ = 0;
  1404.  
  1405.   option_set(sitename, buf);
  1406.  
  1407.   return 0;
  1408. }
  1409.  
  1410. /*-----------------------------command key input-----------------------------*/
  1411. #define K_UP     300
  1412. #define K_DOWN   301
  1413. #define K_RIGHT  302
  1414. #define K_LEFT   303
  1415.  
  1416. int inp_getchr()
  1417. {
  1418.   int c;
  1419.  
  1420.   if ((c = disp_getchar()) != 27 && c != 155)
  1421.     return c;
  1422.  
  1423.   if (c == 27 && (c = disp_getchar()) != '[')
  1424.     return c;
  1425.  
  1426.   switch (c = disp_getchar()) {
  1427.   case 'A':
  1428.     return K_UP;
  1429.   case 'B':
  1430.     return K_DOWN;
  1431.   case 'C':
  1432.     return K_RIGHT;
  1433.   case 'D':
  1434.     return K_LEFT;
  1435.   }
  1436.   return 0;
  1437. }
  1438.  
  1439. /*-------------------------------initial setup-------------------------------*/
  1440. char *setup_showline(entries, n, max)
  1441.   TRANSFER entries[];
  1442.   int n, max;
  1443. {
  1444.   return n < max ? entries[n].text : "";
  1445. }
  1446.  
  1447. LIST setuplist = {(ENTRY **) TransferTypes, setup_showline, 0, 0, 0, 0, 1, 0};
  1448.  
  1449. char *FirstSetupHelp[] = {
  1450.   "ADT initial setup",
  1451.   "It seems that you've never used ADT before. ADT lets you access files",
  1452.   "stored in an Aminet database using various methods (FTP, FSP, mail",
  1453.   "servers and local files). The version you have here may not support all",
  1454.   "of those though. Select your transfer method now.",
  1455.   "NOTE: After setup, you can press 'h' or '?' in most places to get help.",0
  1456. };
  1457.  
  1458. char *SetupHelp[] = {
  1459.   "ADT transfer method setup",
  1460.   "Please select the way ADT should use to access files on Aminet", 0
  1461. };
  1462.  
  1463. int setup_method()
  1464. {
  1465.   TRANSFER *type;
  1466.   int n;
  1467.  
  1468.   setuplist.current = 0;
  1469.   setuplist.num = 0;
  1470.   for (type = TransferTypes; type->text; type++) {
  1471.     if (!mystrcmp(type->name, option_get("method")))
  1472.       setuplist.current = setuplist.num;
  1473.     setuplist.num++;
  1474.   }
  1475.  
  1476.   n = disp_choice(InitialSetup ? FirstSetupHelp : SetupHelp, &setuplist);
  1477.  
  1478.   if (n < 0) {
  1479.     if (InitialSetup)
  1480.       exit_adt("Cannot start up without a transfer method. Goodbye.");
  1481.   } else {
  1482.     if (trans_close)
  1483.       trans_close();
  1484.     option_set("method", TransferTypes[n].name);
  1485.     TransferTypes[n].init();
  1486.     trans_options();
  1487.   }
  1488.  
  1489.   return 0;
  1490. }
  1491.  
  1492.  
  1493. char *DefaultsHelp[] = {
  1494.   "ADT initial setup",
  1495.   "It seems that you've never used ADT before. ADT lets you access files",
  1496.   "stored in ADT databases using various methods (FTP, FSP, mailservers,",
  1497.   "local files. Your sysadmin has selected a default transfer method and",
  1498.   "default site for you. They will be saved as your local configuration",
  1499.   "when you q)uit ADT. You may change them anytime using the o)ptions",
  1500.   "command.",
  1501.   "Press any key to enter ADT", 0
  1502. };
  1503.  
  1504. int setup_defaults()
  1505. {
  1506.   option_set("newest", "0");
  1507.  
  1508.   return 0;
  1509. }
  1510.  
  1511.  
  1512.  
  1513. char *enum_showline(entries, n, max, buf)
  1514.   char **entries, *buf;
  1515.   int n, max;
  1516. {
  1517.   if (n >= max)
  1518.     return "";
  1519.   else
  1520.     return entries[n];
  1521. }
  1522.  
  1523.  
  1524. LIST enumlist = {0, enum_showline, 0, 0, 0, 0, 1, 0};
  1525.  
  1526. int setup_enumed(help, choices, var)
  1527.   char **help, **choices, *var;
  1528. {
  1529.   int n, lines, t;
  1530.  
  1531.   for (lines = 0; choices[lines]; lines++);
  1532.  
  1533.   n = option_getnum(var);
  1534.   if (n < 0)
  1535.     n = 0;
  1536.   if (n >= lines)
  1537.     n = lines - 1;
  1538.  
  1539.   enumlist.list = (ENTRY **) choices;
  1540.   enumlist.num = lines;
  1541.   enumlist.current = n;
  1542.  
  1543.   t = disp_choice(help, &enumlist);
  1544.  
  1545.   if (t >= 0)
  1546.     option_setnum(var, t);
  1547.  
  1548.   return t;
  1549. }
  1550.  
  1551.  
  1552. char *FindMethodHelp[] = {
  1553.   "Find method",
  1554.   "Please choose the method to use for finding files on Aminet. You can use",
  1555.   "an archie-like find server if you have Internet access. If you don't,",
  1556.   "or if you want to make several queries at once, or if your connection to",
  1557.   "Aminet is very fast (e.g. local files), it may be advisable for ADT to",
  1558.   "download the complete file list and do the queries locally. If you only",
  1559.   "want to search what's available at your site, pick the third option.", 0
  1560. }, *FindMethodList[] = {
  1561.   "Remote server search of complete Aminet index",
  1562.   "Local search of current Aminet site",
  1563.   "Local search of complete Aminet index", 0
  1564. };
  1565.  
  1566. char *FindSiteHelp[] = {
  1567.   "Find site selection",
  1568.   "Which server should be used for finding stuff? All servers have a complete",
  1569.   "Aminet index and can thus be used for searches on any of the complete sites", 0
  1570. };
  1571.  
  1572. int setup_findmethod()
  1573. {
  1574.   if (setup_enumed(FindMethodHelp, FindMethodList, "findmethod") == 0)
  1575.     setup_sitelist("findsites", "findsite", FindSiteHelp);
  1576.  
  1577.   option_save(ConfigFile);
  1578.  
  1579.   return 0;
  1580. }
  1581.  
  1582. /* ==============================GENERIC FILE TRANSFER======================== */
  1583. int trans_init()
  1584. {
  1585.   TRANSFER *t = TransferTypes;
  1586.   char *s = option_get("method");
  1587.  
  1588.   while (t->name && mystrcmp(t->name, s))
  1589.     t++;
  1590.  
  1591.   if (!t->name)
  1592.     return 1;
  1593.  
  1594.   t->init();
  1595.  
  1596.   return 0;
  1597. }
  1598.  
  1599.  
  1600. /* ==============================LOCAL FILE PACKAGE=========================== */
  1601. int CdRom;
  1602.  
  1603. int local_connect(progress)
  1604.   int (*progress) ();
  1605. {
  1606.   Connected = 1;
  1607.   return 0;
  1608. }
  1609.  
  1610. int local_remopen(tfh, remote)
  1611.   TEMPFILE *tfh;
  1612.   char *remote;
  1613. {
  1614.   strcpy(tfh->name, option_get("locaminet"));
  1615.   tackon(tfh->name, remote);
  1616.  
  1617.   tfh->cleanup = temp_dummy;
  1618.   tfh->fh = fopen(tfh->name, "r");
  1619.  
  1620.   NoFilesString = "Could not access remote file, use o)ptions s)ite to change path";
  1621.  
  1622.   return 0;
  1623. }
  1624.  
  1625. int local_download(remote, locdir, size, progress)
  1626.   char *remote, *locdir;
  1627.   long size;
  1628.   int (*progress) ();
  1629. {
  1630.   char buf[150], buf2[100];
  1631.  
  1632.   sprintf(buf, "Copying %s", remote);
  1633.   progress(buf);
  1634.  
  1635.   strcpy(buf2, option_get("locaminet"));
  1636.   tackon(buf2, remote);
  1637.  
  1638.   sprintf(buf, "%s %s %s", COPYCMD, buf2, locdir);
  1639.   system(buf);
  1640.  
  1641.   sprintf(buf, "Copied %s", remote);
  1642.   progress(buf);
  1643.  
  1644.   return 0;
  1645. }
  1646.  
  1647. int local_disconnect()
  1648. {
  1649.   Connected = 0;
  1650.   return 0;
  1651. }
  1652.  
  1653. #ifdef AMIGA
  1654. char *LocAminetHelp[] = {
  1655.   "Use a local Aminet database",
  1656.   "Please enter the absolute path to the Aminet files up to the aminet/",
  1657.   "directory, e.g. NET:aminet", 0
  1658. };
  1659.  
  1660. char *CdRomHelp[] = {
  1661.   "Use an Aminet CDROM",
  1662.   "Please enter the absolute path to your Aminet CDROM (or any other non",
  1663.   "updated Aminet file collection), including the Aminet/ end of the path,",
  1664.   "e.g. CD0:Aminet . Note that you need at least Aminet CD 2.", 0
  1665. };
  1666.  
  1667. #else
  1668.  
  1669. char *LocAminetHelp[] = {
  1670.   "Use a local Aminet database",
  1671.   "Please enter the absolute path to the Aminet files. You must not use the",
  1672.   "tilde ~ sign. Enter the path up to the aminet dir, eg. /ftp/pub/aminet .", 0
  1673. };
  1674.  
  1675. char *CdRomHelp[] = {
  1676.   "Please enter the absolute path to your Aminet CDROM (or any other non",
  1677.   "updated Aminet file collection), including the Aminet/ end of the path,",
  1678.   "e.g. CD0:Aminet . Note that you need at least Aminet CD 2.", 0
  1679. };
  1680. #endif
  1681.  
  1682.  
  1683.  
  1684.  
  1685. int local_options()
  1686. {
  1687.   char buf[100];
  1688.  
  1689.   if (InitialSetup)
  1690.     option_set("findmethod", "1");
  1691.  
  1692.  
  1693.   mystrcpy(buf, option_get("locaminet"));
  1694.   disp_inputstr(buf, CdRom ? CdRomHelp : LocAminetHelp);
  1695.   option_set("locaminet", buf);
  1696.  
  1697.   return 0;
  1698. }
  1699.  
  1700. int local_close()
  1701. {
  1702.   return 0;
  1703. }
  1704.  
  1705.  
  1706. int local_init()
  1707. {
  1708.   trans_options = local_options;
  1709.   trans_connect = local_connect;
  1710.   trans_remopen = local_remopen;
  1711.   trans_download = local_download;
  1712.   trans_disconnect = local_disconnect;
  1713.   trans_close = local_close;
  1714.  
  1715.   if ( !strcmp ( option_get("method"), "cdrom"))
  1716.     CdRom=1;
  1717.  
  1718.   return 0;
  1719. }
  1720.  
  1721. /* ==============================EXTERNAL FTP PACKAGE========================= */
  1722. #ifndef NO_EXTERNAL_FTP
  1723.  
  1724. FILE *xFtp;
  1725.  
  1726. int xftp_connect(progress)
  1727.   int (*progress) ();
  1728. {
  1729.   char *site = option_get("ftpsite");
  1730.   char *parts[10],passwd[32];
  1731.   char buf[100];
  1732.  
  1733.   if (Connected)
  1734.     return 0;
  1735.   Connected = 1;
  1736.  
  1737.   str_sitesplit(site, parts);
  1738.  
  1739.   sprintf(buf, "Connecting to %s", parts[1]);
  1740.   progress(buf);
  1741.  
  1742.   sprintf(passwd,"user ftp %s@\n",getlogin());
  1743.  
  1744.   fprintf(xFtp, "open %s\n", parts[1]);
  1745.   fprintf(xFtp, passwd);
  1746.   fprintf(xFtp, "cd pub/aminet\n");
  1747.   fprintf(xFtp, "bin\n");
  1748.  
  1749.   return 0;
  1750. }
  1751.  
  1752. #ifdef AMIGA
  1753. #define xftp_filesize(name) 0
  1754. #else
  1755.  
  1756. static long xftp_filesize(name)
  1757.   char *name;
  1758. {
  1759.   struct stat st;
  1760.  
  1761.   return stat(name, &st) ? -1 : st.st_size;
  1762. }
  1763. #endif
  1764.  
  1765. static int xftp_waitfile(local, size, progress, maxwait, pad, name)
  1766.   char *local;
  1767.   long size;
  1768.   int (*progress) (), maxwait;
  1769.   char *pad, *name;
  1770. {
  1771.   int got, prevgot = -1, timeout = 0;
  1772.   char buf[100];
  1773.  
  1774.   sprintf(buf, "Initiating download of %s", size < 0 ? name : local);
  1775.   progress(buf);
  1776.  
  1777.   while ((got = xftp_filesize(local)) < size || size < 0) {
  1778.  
  1779.     if (got != prevgot)
  1780.       timeout = 0;
  1781.  
  1782.     if (timeout > maxwait)
  1783.       return 1;
  1784.  
  1785.     if (++timeout > 10) {
  1786.       sprintf(buf, "Timeout %d on file %s", maxwait - timeout, local);
  1787.       progress(buf);
  1788.     }
  1789.     prevgot = got;
  1790.     if (got >= 0) {
  1791.       if (size >= 0)
  1792.     sprintf(buf, "Downloading %s, %3dK/%3dK",
  1793.         local, (got + 512) / 1024, (size + 512) / 1024);
  1794.       else
  1795.     sprintf(buf, "Downloading %s, %3dK", name, (got + 512) / 1024);
  1796.  
  1797.       progress(buf);
  1798.     }
  1799.     if (size < 0 && exists(pad))
  1800.       return 0;
  1801.  
  1802.     sleep(1);
  1803.   }
  1804.  
  1805.   return size >= 0 ? got < size : 0;
  1806. }
  1807.  
  1808. int xftp_remopen(tfh, remote, pack, progress)
  1809.   TEMPFILE *tfh;
  1810.   char *remote, *pack;
  1811.   int (*progress) ();
  1812. {
  1813.   char pad[100], buf[100], remname[100];
  1814.  
  1815.   sprintf(remname, "%s%s", remote, pack);
  1816.  
  1817.   temp_name(pad, "PAD");
  1818.   temp_name(tfh->name, "REMOTE");
  1819.   strcat(tfh->name, pack);
  1820.   tfh->cleanup = temp_delete;
  1821.  
  1822.   unlink(pad);
  1823.   unlink(tfh->name);
  1824.  
  1825.   fprintf(xFtp, "get %s %s\n", remname, tfh->name);
  1826.   fprintf(xFtp, "get info/adt/pad %s\n", pad);
  1827.   fflush(xFtp);
  1828.  
  1829.   xftp_waitfile(tfh->name, -1, progress, 30, pad, basename(remname));
  1830.  
  1831.   unlink(pad);
  1832.  
  1833.   if (*pack) {
  1834.     temp_name(tfh->name, "REMOTE");
  1835.     unlink(tfh->name);
  1836.     sprintf(buf, "%s %s", pack[2] ? "gzip -d" : "uncompress", tfh->name);
  1837.     system(buf);
  1838.   }
  1839.   tfh->fh = fopen(tfh->name, "r");
  1840.  
  1841.   return 0;
  1842. }
  1843.  
  1844.  
  1845. int xftp_download(remote, locdir, size, progress)
  1846.   char *remote, *locdir;
  1847.   int size, (*progress) ();
  1848. {
  1849.   char locname[200];
  1850.  
  1851.   strcpy(locname, locdir);
  1852.   tackon(locname, basename(remote));
  1853.  
  1854.   fprintf(xFtp, "get %s %s\n", remote, locname);
  1855.   fflush(xFtp);
  1856.  
  1857.   xftp_waitfile(locname, size, progress, 30, 0);
  1858.  
  1859.   return 0;
  1860. }
  1861.  
  1862.  
  1863. int xftp_disconnect()
  1864. {
  1865.   if (!Connected)
  1866.     return 0;
  1867.   Connected = 0;
  1868.  
  1869.   fprintf(xFtp, "close\n");
  1870.   fflush(xFtp);
  1871.   return 0;
  1872. }
  1873.  
  1874. char *XFTPHelp[] = {
  1875.   "External FTP client setup",
  1876.   "You need a site where you retrieve your files from. Pick one that is",
  1877.   "close to where you live, and later experiment around to find out which",
  1878.   "one's the best for you.", 0
  1879. };
  1880.  
  1881.  
  1882. int xftp_options()
  1883. {
  1884.   setup_sitelist("ftpsites", "ftpsite", XFTPHelp);
  1885.  
  1886.   return 0;
  1887. }
  1888.  
  1889. int xftp_close()
  1890. {
  1891.   if (xFtp) {
  1892.     fprintf(xFtp, "bye\n");
  1893.     pclose(xFtp);
  1894.     xFtp = 0;
  1895.     Connected = 0;
  1896.   }
  1897.   return 0;
  1898. }
  1899.  
  1900. int xftp_init()
  1901. {
  1902.   char buf[100];
  1903.  
  1904.   sprintf(buf, "ftp -n");
  1905.  
  1906.   if (!xFtp && !(xFtp = popen(buf, "w")))
  1907.     exit_adt("Could not start 'ftp'");
  1908.  
  1909.   trans_options = xftp_options;
  1910.   trans_connect = xftp_connect;
  1911.   trans_remopen = xftp_remopen;
  1912.   trans_download = xftp_download;
  1913.   trans_disconnect = xftp_disconnect;
  1914.   trans_close = xftp_close;
  1915.  
  1916.   return 0;
  1917. }
  1918.  
  1919. #endif
  1920.  
  1921.  
  1922. /* ==============================TCP functions========================= */
  1923. #ifndef NO_TCP_UTILITY
  1924.  
  1925. /* creates an new tcp socket and connects it to <host> on port <port>. returns
  1926.  * -1 if a failure occured. NOTE: host can either be a hostname or a
  1927.  * dot-notation of the internet adress */
  1928. int tcp_createsocket(host, port)
  1929.   char *host;
  1930.   int port;
  1931. {
  1932.   struct sockaddr_in sin;
  1933.   struct hostent *h;
  1934.   int connected, net = -1;
  1935.  
  1936.   if ((sin.sin_addr.s_addr = inet_addr(host)) == -1) {
  1937.     if (!(h = gethostbyname(host)))
  1938.       return -1;
  1939.     else {
  1940.       sin.sin_family = h->h_addrtype;
  1941.  
  1942. #ifndef NOT43
  1943.       memcpy((caddr_t) & sin.sin_addr, h->h_addr_list[0], h->h_length);
  1944. #else
  1945.       memcpy((caddr_t) & sin.sin_addr, h->h_addr, h->h_length);
  1946. #endif
  1947.     }
  1948.   } else
  1949.     sin.sin_family = AF_INET;
  1950.  
  1951.   sin.sin_port = htons(port);
  1952.  
  1953.   for (connected = 1; connected && (connected < 30); connected++) {
  1954.     if ((net = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  1955.       return -1;
  1956.  
  1957.     if (connect(net, (struct sockaddr *) & sin, sizeof(sin)) >= 0) {
  1958.       connected = 0;
  1959.       break;
  1960.     } else
  1961.       close(net);
  1962.   }
  1963.  
  1964.   if (!connected)
  1965.     return net;
  1966.   else
  1967.     return -1;
  1968. }
  1969.  
  1970. #endif
  1971.  
  1972. /* ==============================BUILTIN FTP PACKAGE========================= */
  1973. #ifndef NO_BUILTIN_FTP
  1974.  
  1975. #ifndef BFTP_BUFSIZE
  1976. # define BFTP_BUFSIZE 10000
  1977. #endif
  1978.  
  1979. FILE *bftp_io = NULL;
  1980.  
  1981. char *BFTPHelp[] =
  1982. {
  1983.   "Builtin FTP client setup",
  1984.   "You'll need a site where you retrieve your files from. Pick one that is",
  1985.   "close to where you live, and later experiment around to find out which",
  1986.   "one's the best for you.", 0
  1987. };
  1988.  
  1989. int bftp_options()
  1990. {
  1991.   return setup_sitelist("ftpsites", "ftpsite", BFTPHelp);
  1992. }
  1993.  
  1994. int bftp_disconnect()
  1995. {
  1996.   Connected = 0;
  1997.  
  1998.   if (bftp_io)
  1999.     fclose(bftp_io);
  2000.   bftp_io = NULL;
  2001.  
  2002.   return 0;
  2003. }
  2004.  
  2005. int bftp_close()
  2006. {
  2007.   return bftp_disconnect();
  2008. }
  2009.  
  2010. /* Send FTP command and wait for reply. Returns 0 on failure. */
  2011. int bftp_cmd(cmd)
  2012.   char *cmd;
  2013. {
  2014.   char buf[256];
  2015.   int result;
  2016.  
  2017.   if (!bftp_io)
  2018.     return 0;
  2019.  
  2020.   if (cmd) {
  2021.     fflush(bftp_io);
  2022.     rewind(bftp_io);
  2023.     fputs(cmd, bftp_io);
  2024.   }
  2025.   fflush(bftp_io);
  2026.   rewind(bftp_io);
  2027.   if (!fgets(buf, sizeof(buf), bftp_io))
  2028.     return 0;
  2029.  
  2030.   result = myatoi(buf);
  2031.  
  2032.   while ((buf[3] != ' ') || (myatoi(buf) != result)) {
  2033.     if (!fgets(buf, sizeof(buf), bftp_io))
  2034.       return 0;
  2035.   }
  2036.  
  2037.   return (result >= 100) && (result < 400);
  2038. }
  2039.  
  2040. int bftp_getdataconnection()
  2041. {
  2042.   struct sockaddr_in sin;
  2043.   struct hostent *hp;
  2044.   int d, i;
  2045.   unsigned char *a, *p;
  2046.   char buf[80];
  2047.  
  2048.   i = sizeof(sin);
  2049.   if (getsockname (fileno(bftp_io), (struct sockaddr *)&sin, &i))
  2050.     return -1;
  2051.  
  2052.   /* clear Port, let system choose */
  2053.   sin.sin_port = 0;
  2054.  
  2055.   if ((d = socket(sin.sin_family, SOCK_STREAM, 0)) < 0)
  2056.     return -1;
  2057.  
  2058.   i = sizeof(sin);
  2059.   if ((bind(d, (struct sockaddr *) & sin, i) < 0) ||
  2060.       (getsockname(d, (struct sockaddr *) & sin, &i) < 0) ||
  2061.       (listen(d, 1) < 0)) {
  2062.     close(d);
  2063.     return -1;
  2064.   }
  2065.   a = (unsigned char *) &sin.sin_addr;
  2066.   p = (unsigned char *) &sin.sin_port;
  2067.  
  2068.   sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\n", a[0], a[1], a[2], a[3], p[0], p[1]);
  2069.   if (!bftp_cmd(buf)) {
  2070.     close(d);
  2071.     return -1;
  2072.   }
  2073.   return d;
  2074. }
  2075.  
  2076. int bftp_getfile(remote, local, size, progress)
  2077.   char *remote, *local;
  2078.   int size, (*progress) ();
  2079. {
  2080.   FILE *out = NULL;
  2081.   int s = -1, d = -1, i, got = 0, result = 0;
  2082.   char *buf, *msg = NULL, tmp[80];
  2083.   struct sockaddr_in sin;
  2084.  
  2085.   if (!Connected)
  2086.     return 0;
  2087.  
  2088.   buf = salloc(BFTP_BUFSIZE);
  2089.  
  2090.   sprintf(buf, "Getting %s...", remote);
  2091.   progress(buf);
  2092.  
  2093.   if ((s = bftp_getdataconnection()) >= 0) {
  2094.     sprintf(buf, "RETR %s\n", remote);
  2095.     if (bftp_cmd(buf)) {
  2096.       i = sizeof(sin);
  2097.       if ((d = accept(s, (struct sockaddr *) & sin, &i)) >= 0) {
  2098.     if ((out = fopen(local, "w"))) {
  2099.       int len, k, start = time(NULL), t;
  2100.       float kps;
  2101.  
  2102.       while ((len = read(d, buf, BFTP_BUFSIZE)) > 0) {
  2103.         if (fwrite(buf, len, 1, out) != 1) {
  2104.           close(s);
  2105.           fclose(out);
  2106.           msg = "Error writing to local file";
  2107.           break;
  2108.         }
  2109.         got += len;
  2110.  
  2111.         if (progress) {
  2112.           k = (got + 512) / 1024;
  2113.           kps = (float)got / (float)((t = time(NULL) - start) ? t * 1024 : 1024);
  2114.           kps = (int)(kps*10.0) / 10.0;
  2115.  
  2116.           if (size > 0)
  2117.         sprintf(tmp, "Downloading %s, %4dK/%4dK (%#3.3g Kbyte/s)",
  2118.             remote, k, (size + 512) / 1024, kps);
  2119.           else
  2120.         sprintf(tmp, "Downloading %s, %4dK (%#3.3g Kbyte/s)", remote, k, kps);
  2121.  
  2122.           progress(tmp);
  2123.         }
  2124.       }
  2125.  
  2126.       if (bftp_cmd(NULL) && ((size <= 0) || (got == size)))
  2127.         result = 1;
  2128.       else {
  2129.         sprintf(buf, "Error getting file %s (got %d bytes instead of %d)", 
  2130.             remote, got, size);
  2131.         msg = buf;
  2132.       }
  2133.     } else
  2134.       msg = "Cannot create local file";
  2135.       } else
  2136.     msg = "Cannot establish data connection to ftp site";
  2137.     } else
  2138.       msg = "Cannot get file from ftp site";
  2139.   } else
  2140.     msg = "Cannot initiate data connection to ftp site";
  2141.  
  2142.   if (progress && msg) {
  2143.     sleep(1);
  2144.     progress(msg);
  2145.     sleep(2);
  2146.   }
  2147.  
  2148.   if (d >= 0)
  2149.     close(d);
  2150.   if (s >= 0)
  2151.     close(s);
  2152.   if (out)
  2153.     fclose(out);
  2154.   free(buf);
  2155.  
  2156.   return result;
  2157. }
  2158.  
  2159. int bftp_connect(progress, remote)
  2160.   int (*progress) ();
  2161.   int remote;
  2162. {
  2163.   char *parts[10], buf[100], passwd[32] , *msg = NULL;
  2164.   struct servent *sp;
  2165.   int s, port = 21;
  2166.   char *site;
  2167.  
  2168.   if (Connected)
  2169.     return 1;
  2170.   Connected = 1;
  2171.  
  2172.   str_sitesplit(option_get("ftpsite"), parts);
  2173.  
  2174.   if (remote)
  2175.     sprintf(buf, "Connecting to %s (master site)", site=parts[*parts[4]? 4:1]);
  2176.   else
  2177.     sprintf(buf, "Connecting to %s", site=parts[1]);
  2178.  
  2179.   progress(buf);
  2180.  
  2181.   if ((sp = getservbyname("ftp", "tcp")))
  2182.     port = ntohs(sp->s_port);
  2183.  
  2184.   sprintf(passwd,"PASS %s@\n",getlogin());             /* take users login name as passwd */
  2185.  
  2186.   if ((s = tcp_createsocket(site, port)) >= 0) {
  2187.     if ((bftp_io = fdopen(s, "r+"))) {
  2188.       sprintf (buf, "CWD %s\n", *parts[3] ? parts[3] : "pub/aminet");
  2189.       if (bftp_cmd(NULL) &&
  2190.       bftp_cmd("USER ftp\n") &&
  2191.       bftp_cmd(passwd) &&
  2192.       bftp_cmd( buf ) &&
  2193.       bftp_cmd("TYPE I\n"))
  2194.     return 0;
  2195.       else
  2196.     msg = "Login failed, probably user limit reached, retry later.";
  2197.     } else {
  2198.       close(s);
  2199.       msg = "Could not communicate with ftp site.";
  2200.     }
  2201.   } else
  2202.     msg = "Could not connect to ftp site.";
  2203.  
  2204.   bftp_close();
  2205.  
  2206.   if (msg) {
  2207.     sleep(1);
  2208.     progress(msg);
  2209.     sleep(2);
  2210.   }
  2211.  
  2212.   return 1;
  2213. }
  2214.  
  2215. int bftp_remopen(tfh, remote, pack, progress)
  2216.   TEMPFILE *tfh;
  2217.   char *remote, *pack;
  2218.   int (*progress) ();
  2219. {
  2220.   char buf[200];
  2221.  
  2222.   tfh->fh = NULL;
  2223.   temp_name(tfh->name, "RECENT");
  2224.   strcat(tfh->name, pack);
  2225.   tfh->cleanup = temp_delete;
  2226.  
  2227.   unlink(tfh->name);
  2228.  
  2229.   sprintf(buf, "%s%s", remote, pack);
  2230.  
  2231.   if (bftp_getfile(buf, tfh->name, 0, progress)) {
  2232.     if (*pack) {
  2233.       temp_name(tfh->name, "RECENT");
  2234.       unlink(tfh->name);
  2235.  
  2236.       progress ("Uncompressing");
  2237.       if (pack[2]) {
  2238.         sprintf(buf,"gzip -d <%s%s >%s", tfh->name, pack, tfh->name);
  2239.       } else {
  2240.         sprintf(buf,"uncompress <%s%s >%s", tfh->name, pack, tfh->name);
  2241.       }
  2242.       system(buf);
  2243.       sprintf(buf,"%s%s",tfh->name,pack);
  2244.       unlink (buf);
  2245.     }
  2246.  
  2247.     tfh->fh = fopen(tfh->name, "r");
  2248.   }
  2249.   NoFilesString = "Could not download index file";
  2250.  
  2251.   return 0;
  2252. }
  2253.  
  2254. int bftp_download(remote, locdir, size, progress)
  2255.   char *remote, *locdir;
  2256.   int size, (*progress) ();
  2257. {
  2258.   char buf[200];
  2259.  
  2260.   strcpy(buf, locdir);
  2261.   tackon(buf, basename(remote));
  2262.  
  2263.   bftp_getfile(remote, buf, size, progress);
  2264.  
  2265.   return 0;
  2266. }
  2267.  
  2268. int bftp_init()
  2269. {
  2270.   Connected = 0;
  2271.   trans_options = bftp_options;
  2272.   trans_connect = bftp_connect;
  2273.   trans_disconnect = bftp_disconnect;
  2274.   trans_remopen = bftp_remopen;
  2275.   trans_download = bftp_download;
  2276.   trans_close = bftp_close;
  2277.  
  2278.   return 0;
  2279. }
  2280.  
  2281. #endif
  2282.  
  2283. /* =================================FIND CLIENT=============================== */
  2284. #ifndef NO_FIND_CLIENT
  2285.  
  2286. #define FINDD_PORT 1848
  2287.  
  2288. FILE *findd_fgrep(host, string, max)
  2289.   char *host, *string;
  2290.   int max;
  2291. {
  2292.   int fd;
  2293.   char buf[200];
  2294.   int port= FINDD_PORT;
  2295.  
  2296.   sprintf(buf, "Connecting to findserver %s...", host);
  2297.   disp_clear();
  2298.   disp_report(buf);
  2299.  
  2300.   if ((fd = tcp_createsocket(host, port)) >= 0) {
  2301.     sprintf(buf, "max %d ; ADTfind %s ; quit\r\n", max, string);
  2302.     write(fd, buf, strlen(buf));
  2303.     return fdopen(fd, "r");
  2304.   } else {
  2305.     sleep(1);
  2306.     disp_report("Could not connect to find server");
  2307.     sleep(2);
  2308.   }
  2309.  
  2310.   return NULL;
  2311. }
  2312.  
  2313.  
  2314. #else
  2315.  
  2316. char *NoFinddHelp[] =
  2317. {
  2318.   "Sorry, no find daemon compiled into this version", 0
  2319. };
  2320.  
  2321. FILE *findd_fgrep(host, port, string, max)
  2322.   char *host, *string;
  2323.   int port, max;
  2324. {
  2325.   disp_confirm(NoFinddHelp, 0);
  2326.   return NULL;
  2327. }
  2328.  
  2329. #endif
  2330.  
  2331. /* =================================MAIN PROGRAM============================== */
  2332.  
  2333. int adt_connect (progress, remote)
  2334.   int (*progress) ();
  2335.   int remote;
  2336. {
  2337.   return trans_connect (progress, remote);
  2338. }
  2339.  
  2340. int adt_disconnect ()
  2341. {
  2342.   trans_disconnect ();
  2343.   return 0;
  2344. }
  2345.  
  2346. #ifdef SIGINT
  2347. void adt_breakcheck (sig)
  2348.   int sig;
  2349. {
  2350.   signal(SIGINT, adt_breakcheck);
  2351.  
  2352.   if (!MaskSIGINT || (MaskSIGINT && GotSIGINT))
  2353.     exit_adt ("Aborted");
  2354.   GotSIGINT=1;
  2355. }
  2356. #endif
  2357.  
  2358.  
  2359. int tagcurrent()
  2360. {
  2361.   if (Vis->num && Vis->list[Vis->current])
  2362.     Vis->list[Vis->current]->tagged ^= 1;
  2363.  
  2364.   return 0;
  2365. }
  2366.  
  2367. int tagall()
  2368. {
  2369.   int tag, i;
  2370.   ENTRY **e = Vis->list;
  2371.  
  2372.   if (Vis->num) {
  2373.     tag = Vis->list[0]->tagged ^ 1;
  2374.     for (i = 0; i < Vis->num; i++)
  2375.       e[i]->tagged = tag;
  2376.   }
  2377.   return 0;
  2378. }
  2379.  
  2380. char transferdesc[200];
  2381.  
  2382. char *transferstr()
  2383. {
  2384.   char *proc = Action ? (Action == 2 ? "unpack" : "send") : "none";
  2385.  
  2386.   sprintf(transferdesc,
  2387.       "Action: %-6s  Path: %-16.16s  Readmes: %c  Subdirs: %c  Verbose: %c",
  2388.       proc, Path, Readme ? 'y' : 'n', Flat ? 'n' : 'y', Silent ? 'n' : 'y');
  2389.  
  2390.   return transferdesc;
  2391. }
  2392.  
  2393. int progress_dummy()
  2394. {
  2395.   return 0;
  2396. }
  2397.  
  2398. int make_dirs(path)
  2399.   char *path;
  2400. {
  2401.   char buf[200], *s = path, *d = buf;
  2402.  
  2403.   do {
  2404.     while (*s && *s != '/')
  2405.       *d++ = *s++;
  2406.  
  2407.     *d = 0;
  2408.  
  2409.     if (access(buf, 3))
  2410.       makedir(buf, 0755);
  2411.  
  2412.     if (*s)
  2413.       *d++ = *s++;
  2414.   } while (*s);
  2415.  
  2416.   return 0;
  2417. }
  2418.  
  2419.  
  2420. int do_action(locdir, remname)
  2421.   char *locdir, *remname;
  2422. {
  2423.   char locname[200];
  2424.   char buf[200];
  2425.   int len;
  2426.  
  2427.   strcpy(locname, locdir);
  2428.   tackon(locname, basename(remname));
  2429.  
  2430.   if (Action == 1) {
  2431.     sprintf(buf, "%s %s", option_get("send"), locname);
  2432.     system(buf);
  2433.     sleep(2);
  2434.     disp_refresh();
  2435.   }
  2436.   if (Action == 2) {
  2437.     len = mystrlen(locname);
  2438.  
  2439.     if (len > 4 && !mystricmp(locname + len - 4, ".lha")) {
  2440.       puts("");
  2441.       sprintf(buf, "cd %s;lha e %s", locdir, locname);
  2442.       system(buf);
  2443.       disp_refresh();
  2444.     } else if (len > 2 && !mystrcmp(locname + len - 2, ".Z")) {
  2445.       puts("");
  2446.       sprintf(buf, "cd %s;uncompress %s", locdir, locname);
  2447.       system(buf);
  2448.       disp_refresh();
  2449.     } else if (len > 3 && !mystrcmp(locname + len - 3, ".gz")) {
  2450.       puts("");
  2451.       sprintf(buf, "cd %s;gzip -d %s", locdir, locname);
  2452.       system(buf);
  2453.       disp_refresh();
  2454.     }
  2455.   }
  2456.   return 0;
  2457. }
  2458.  
  2459. int get_files(progress, showfile)
  2460.   int (*progress) (), (*showfile) ();
  2461. {
  2462.   ENTRY *e;
  2463.   char locdir[200], remname[200];
  2464.   int i, p, err = 0, n;
  2465.  
  2466.   if (Silent)
  2467.     progress = progress_dummy;
  2468.  
  2469.  
  2470.   MaskSIGINT = 1;
  2471.  
  2472.   for (p=0; p<2; p++) {
  2473.  
  2474.     for (i = n = 0; i < Vis->num; i++)
  2475.       if ( (Vis->list[i]->status & 1) == p)
  2476.         n++;
  2477.  
  2478.     if (!n)
  2479.       continue;
  2480.  
  2481.     if (adt_connect(progress, p))
  2482.       continue;
  2483.  
  2484.     for (i = 0; i < Vis->num; i++) {
  2485.       e = Vis->list[i];
  2486.  
  2487.       if ((e->status & 1) != p)
  2488.         continue;
  2489.  
  2490.       showfile (Vis, i);
  2491.  
  2492.       mystrcpy(locdir, option_get("dlpath"));
  2493.       glob_path(locdir);
  2494.       if (!Flat)
  2495.         tackon(locdir, e->dir);
  2496.       make_dirs(locdir);
  2497.  
  2498.       strcpy(remname, e->dir);
  2499.       tackon(remname, e->file);
  2500.  
  2501.       if (!*locdir)
  2502.         strcpy(locdir, CURDIR);
  2503.  
  2504.       if ((err = trans_download(remname, locdir, e->size, progress)))
  2505.         break;
  2506.  
  2507.       if ( GotSIGINT )
  2508.         break;
  2509.  
  2510.       do_action(locdir, remname);
  2511.  
  2512.       if (Readme) {
  2513.         strcpy(remname, e->dir); 
  2514.         tackon(remname, get_readme(e));
  2515.  
  2516.         if ((err = trans_download(remname, locdir, e->readmesize, progress)))
  2517.       break;
  2518.  
  2519.         if ( GotSIGINT )
  2520.           break;
  2521.  
  2522.         do_action(locdir, remname);
  2523.       }
  2524.  
  2525.       if (GotSIGINT) {
  2526.         progress ("Interrupted");
  2527.         break;
  2528.       }
  2529.  
  2530.       e->tagged = 0;
  2531.       showfile (Vis, i);
  2532.  
  2533.     }
  2534.  
  2535.     adt_disconnect();
  2536.  
  2537.     if (err)
  2538.       disp_status("Error during download");
  2539.  
  2540.   }
  2541.  
  2542.   MaskSIGINT = GotSIGINT = 0;
  2543.  
  2544.   return 0;
  2545. }
  2546.  
  2547. int find_string(string, progress)
  2548.   char *string;
  2549.   int (*progress) ();
  2550. {
  2551.   FILE *fh;
  2552.   char *site;
  2553.   char *parts[10];
  2554.  
  2555.   NoFilesString = "No files found matching your pattern";
  2556.   Vis->sel = "Found files";
  2557.  
  2558.   if (!*option_get("findmethod"))
  2559.     if (setup_findmethod())
  2560.       return 1;
  2561.  
  2562.   switch (option_getnum("findmethod")) {
  2563.  
  2564.   case 0:
  2565.  
  2566.     GotComplete = GotLocal = GotRecent = 0;
  2567.  
  2568.     site = option_get("findsite");
  2569.  
  2570.     progress("Searching...");
  2571.  
  2572.     str_sitesplit(site, parts);
  2573.  
  2574.     fh = findd_fgrep(parts[1], string, 100);
  2575.  
  2576.     if (fh) {
  2577.       e_freeall();
  2578.       fgets(parsebuf, 120, fh);
  2579.       read_adt_v2(fh);
  2580.       allvisible();
  2581.       fclose(fh);
  2582.     }
  2583.     Vis->sel = "Found files";
  2584.     sort_list(Vis, &sort_dir);
  2585.  
  2586.     break;
  2587.  
  2588.   case 1:
  2589.  
  2590.     get_sitelocal();
  2591.     progress("Searching...");
  2592.     findstr(string);
  2593.     break;
  2594.  
  2595.   case 2:
  2596.  
  2597.     get_complete();
  2598.     progress("Searching...");
  2599.     findstr(string);
  2600.     break;
  2601.  
  2602.   }
  2603.  
  2604.   return 0;
  2605. }
  2606.  
  2607. int print_listing(width)
  2608.   int width;
  2609. {
  2610.   ENTRY **list = Vis->list;
  2611.   FILE *f;
  2612.   char file[300];
  2613.   int i;
  2614.   long now = time(NULL);
  2615.  
  2616.   strcpy (file, glob_path( option_get("printer")));
  2617.  
  2618.   if (!*file || !(f = fopen(file, "w")))
  2619.     return 0;
  2620.  
  2621.   if (width == 1) {
  2622.  
  2623.     fputs("|File                Dir        Size Description\n", f);
  2624.     fputs("|------------------- ---------- ---- -----------\n", f);
  2625.  
  2626.   } else {
  2627.  
  2628.     fputs("|File                Dir        Size Age  Description\n", f);
  2629.     fputs("|------------------- ---------- ---- ---  -----------\n", f);
  2630.  
  2631.   }
  2632.  
  2633.  
  2634.   for (i = 0; i < Vis->num; i++) {
  2635.  
  2636.     if (width == 1) {
  2637.  
  2638.       fprintf(f, "%-20.20s %-10.10s %4.4s%c%-42.42s\n",
  2639.           list[i]->file,
  2640.           list[i]->dir,
  2641.           str_ksize(list[i]->size),
  2642.           list[i]->readmesize > 100 ? '+' : ' ',
  2643.           list[i]->desc);
  2644.  
  2645.     } else {
  2646.  
  2647.       fprintf(f, "%-20.20s %-10.10s %4.4s %3.3d %c%-.80s\n",
  2648.           list[i]->file,
  2649.           list[i]->dir,
  2650.           str_ksize(list[i]->size),
  2651.           (now - list[i]->time) / 86400,
  2652.           list[i]->readmesize > 100 ? '+' : ' ',
  2653.           list[i]->desc);
  2654.  
  2655.     }
  2656.   }
  2657.  
  2658.   fclose(f);
  2659.  
  2660.   return 0;
  2661. }
  2662.  
  2663.  
  2664. int display_remote(name, title)
  2665.   char *name;
  2666.   char *title;
  2667. {
  2668.   TEMPFILE tfh;
  2669.   char **strings;
  2670.  
  2671.   trans_remopen(&tfh, name, "", disp_status);
  2672.  
  2673.   if (tfh.fh) {
  2674.  
  2675.     strings = read_file(tfh.fh, title);
  2676.     disp_more(strings);
  2677.     free_file(strings);
  2678.     tclose(&tfh);
  2679.  
  2680.   } else {
  2681.  
  2682.     disp_status ("Could not open remote file");
  2683.  
  2684.   }
  2685.  
  2686.   return 0;
  2687. }
  2688.  
  2689.  
  2690. /* =================================CURSES PACKAGE============================ */
  2691. #ifndef NO_CURSES
  2692.  
  2693. int debug(str)
  2694.   char *str;
  2695. {
  2696.   move(0, 0);
  2697.   addstr(str);
  2698.   refresh();
  2699.   sleep(1);
  2700.   return 0;
  2701. }
  2702.  
  2703. int cur_dispinit()
  2704. {
  2705.   initscr();
  2706.   nl();
  2707.   noecho();
  2708.   cbreak();
  2709.   return 0;
  2710. }
  2711.  
  2712. int cur_getchar()
  2713. {
  2714.   int c=getchr();
  2715. #ifdef SHOW_KEYS
  2716.   static int foo;
  2717.   char buf[20];
  2718.   sprintf(buf,"%2x",c);
  2719.   move(0, (foo+=2)%8);
  2720.   addstr(buf);
  2721. #endif
  2722.  
  2723.   return c;
  2724. }
  2725.  
  2726.  
  2727. #define Y_LISTTITLE 2
  2728. #define Y_LISTSTART 3
  2729. #define Y_LISTEND   (LINES-6)
  2730. #define Y_INFO      (LINES-5)
  2731. #define Y_COMMAND   (LINES-3)
  2732. #define Y_STATUS    (LINES-1)
  2733.  
  2734. #define X_COMMAND   9
  2735.  
  2736.  
  2737. int cur_helpblock(help)
  2738.   char **help;
  2739. {
  2740.   int i, m = str_shift(str_longest(help), COLS);
  2741.  
  2742.   clear();
  2743.  
  2744.   move(0, 0);
  2745.   addstr(str_center(help[0], COLS));
  2746.  
  2747.   for (i = 0; help[i + 1]; i++) {
  2748.     move(2 + i, m);
  2749.     addstr(help[i + 1]);
  2750.   }
  2751.  
  2752.   return i;
  2753. }
  2754.  
  2755. char *cur_getstringat(buf, x, y)
  2756.   char *buf;
  2757.   int x, y;
  2758. {
  2759.   char bak[300];
  2760.   int c, l = strlen(buf), p = l, i;
  2761.  
  2762.   mystrcpy(bak, buf);
  2763.  
  2764.   move(y, x);
  2765.   addstr(buf);
  2766.  
  2767.   for (;;) {
  2768.     move(y, x + p);
  2769.     refresh();
  2770.     switch (c = inp_getchr()) {
  2771.  
  2772.     case 1:
  2773.       p = 0;
  2774.       break;
  2775.  
  2776.     case 5:
  2777.       p = l;
  2778.       break;
  2779.  
  2780.     case 8: case 127:
  2781.       if (p == 0)
  2782.     break;
  2783.  
  2784.       for (i = --p; i < l; i++)
  2785.     buf[i] = buf[i + 1];
  2786.       buf[--l] = ' ';
  2787.  
  2788.       move(y, x + p);
  2789.       addstr(buf + p);
  2790.       buf[l] = 0;
  2791.       break;
  2792.  
  2793.     case 13: case 10:
  2794.       goto done;
  2795.  
  2796.     case K_RIGHT:
  2797.       if (p < l)
  2798.     p++;
  2799.       break;
  2800.  
  2801.     case K_LEFT:
  2802.       if (p > 0)
  2803.     p--;
  2804.       break;
  2805.  
  2806.     default:
  2807.       if (c < ' ' || c > 127 || l > COLS - x - 1)
  2808.     break;
  2809.  
  2810.       for (i = l; i > p; i--)
  2811.     buf[i + 1] = buf[i];
  2812.       buf[++l] = 0;
  2813.       buf[p] = c;
  2814.  
  2815.       move(y, x + p);
  2816.       addstr(buf + p);
  2817.       p++;
  2818.       break;
  2819.     }
  2820.  
  2821.   }
  2822.  
  2823. done:
  2824.   return buf;
  2825.  
  2826. }
  2827.  
  2828. int OptLen;
  2829.  
  2830. char *cur_inputstr(buf, help)
  2831.   char *buf, **help;
  2832. {
  2833.   int x = str_shift(str_longest(help), COLS);
  2834.   int y = 3 + cur_helpblock(help);
  2835.  
  2836.   move(y, x);
  2837.   addstr("> ");
  2838.  
  2839.   return cur_getstringat(buf, x + 2, y);
  2840. }
  2841.  
  2842.  
  2843. char *cur_getstring(buf)
  2844.   char *buf;
  2845. {
  2846.   int y = Y_COMMAND, x = X_COMMAND + OptLen;
  2847.   char *ret;
  2848.  
  2849.   move(y, x);
  2850.   addstr(": ");
  2851.   x += 2;
  2852.   OptLen += 2;
  2853.  
  2854.   ret = cur_getstringat(buf, x, y);
  2855.  
  2856.   OptLen = x + strlen(buf) + 1;
  2857.  
  2858.   return ret;
  2859. }
  2860.  
  2861. char dbuf[300];
  2862.  
  2863. int cur_displist(list)
  2864.   LIST *list;
  2865. {
  2866.   int i;
  2867.   char *line;
  2868.   char *t = NULL, c = 0;
  2869.  
  2870.   Window = Y_LISTEND - list->top;
  2871.  
  2872.   if (!list->num) {
  2873.     move(LINES / 2, 0);
  2874.     addstr(str_center(NoFilesString, COLS - 1));
  2875.     return 0;
  2876.   }
  2877.  
  2878.   list->offset = list->current / Window * Window;
  2879.  
  2880.   for (i = 0; i < Window; i++) {
  2881.     line = list->disp(list->list, list->offset + i, list->num, dbuf, Width);
  2882.  
  2883.     if (*line)
  2884.       move(list->top + i, list->type ? (Width - strlen(line)) / 2 : 0);
  2885.     else {
  2886.       move(list->top + i, 0);
  2887.       clrtoeol();
  2888.     }
  2889.  
  2890.     if (list->offset + i != list->current) {
  2891.       addstr(line);
  2892.     } else {
  2893.       if (list->type == 0) {
  2894.     for (t = line + 1; *t != ' '; t++);
  2895.     c = t[1];
  2896.     t[1] = 0;
  2897.       }
  2898.       standout();
  2899.       addstr(line);
  2900.       standend();
  2901.  
  2902.       if (list->type == 0) {
  2903.     t[1] = c;
  2904.     addstr(t + 1);
  2905.       }
  2906.     }
  2907.   }
  2908.  
  2909.   return 0;
  2910. }
  2911.  
  2912. int cur_dispfiles(list)
  2913.   LIST *list;
  2914. {
  2915.   Window = Y_LISTEND - Y_LISTSTART;
  2916.  
  2917.   if (list == Vis) {
  2918.     move(0, 0);
  2919.     addstr(str_topline(dbuf, Width - 1, list));
  2920.   }
  2921.  
  2922.   cur_displist(list);
  2923.  
  2924.   return 0;
  2925. }
  2926.  
  2927.  
  2928. int cur_dispmask()
  2929. {
  2930.   Window = Y_LISTEND - Y_LISTSTART;
  2931.  
  2932.   Vis->disp = str_entry;
  2933.   Vis->top = Y_LISTSTART;
  2934.  
  2935.   clear();
  2936.   move(Y_LISTTITLE, 0);
  2937.   addstr(str_titles(dbuf, Width - 1));
  2938.  
  2939.   cur_dispfiles(Vis);
  2940.  
  2941.   move(Y_INFO, 0);
  2942.   addstr(str_center(Vis->cmds, Width - 1));
  2943.  
  2944.   move(Y_COMMAND, 0);
  2945.   addstr("Command: ");
  2946.  
  2947.   refresh();
  2948.   return 0;
  2949. }
  2950.  
  2951. int cur_mark(list, on)
  2952.   LIST *list;
  2953.   int on;
  2954. {
  2955.   char *t = NULL, *line;
  2956.  
  2957.   line = list->disp(list->list, list->current, list->num, dbuf, Width - 1);
  2958.  
  2959.   if (list->type == 0) {
  2960.     for (t = line + 1; *t != ' '; t++);
  2961.     t[1] = 0;
  2962.   }
  2963.   move(list->top + list->current - list->offset,
  2964.        list->type ? (Width - strlen(line)) / 2 : 0);
  2965.  
  2966.   if (on)
  2967.     standout();
  2968.   addstr(line);
  2969.   if (on)
  2970.     standend();
  2971.  
  2972.   if (list->type == 0)
  2973.     t[1] = ' ';
  2974.  
  2975.   return 0;
  2976. }
  2977.  
  2978. int cur_gomid(list)
  2979.   LIST *list;
  2980. {
  2981.   int left = list->num - list->offset;
  2982.  
  2983.   list->current = list->offset + (left < Window ? left : Window) / 2;
  2984.   return 0;
  2985. }
  2986.  
  2987.  
  2988. int cur_moveto(list, pos)
  2989.   LIST *list;
  2990.   int pos;
  2991. {
  2992.   if (!list->num)
  2993.     return 0;
  2994.  
  2995.   if (pos < 0)
  2996.     pos = 0;
  2997.  
  2998.   if (pos > list->num - 1)
  2999.     pos = list->num - 1;
  3000.  
  3001.   if (pos == list->current)
  3002.     return 0;
  3003.  
  3004.   cur_mark(list, 0);
  3005.   list->current = pos;
  3006.  
  3007.   if (pos / Window * Window == list->offset) {
  3008.  
  3009.     cur_mark(list, 1);
  3010.  
  3011.   } else {
  3012.  
  3013.     list->offset = pos / Window * Window;
  3014.     cur_dispfiles(list);
  3015.  
  3016.   }
  3017.  
  3018.   return 0;
  3019. }
  3020.  
  3021. int cur_listnav(list, c)
  3022.   LIST *list;
  3023.   int c;
  3024. {
  3025.   switch (c) {
  3026.   case K_DOWN:
  3027.   case K_CTRL + 'N':
  3028.   case 'j':
  3029.     cur_moveto(list, list->current + 1);
  3030.     break;
  3031.  
  3032.   case K_UP:
  3033.   case K_CTRL + 'P':
  3034.   case 'k':
  3035.     cur_moveto(list, list->current - 1);
  3036.     break;
  3037.  
  3038.   case K_RIGHT:
  3039.   case ' ':
  3040.   case K_CTRL + 'F':
  3041.     cur_moveto(list, (list->current + Window)/Window*Window);
  3042.     break;
  3043.  
  3044.   case K_LEFT:
  3045.   case K_CTRL + 'H':
  3046.   case K_CTRL + 'B':
  3047.   case 'b':
  3048.     cur_moveto(list, (list->current - Window)/Window*Window);
  3049.     break;
  3050.  
  3051.   case K_CTRL + 'I':
  3052.     cur_moveto(list, list->current + 5);
  3053.     break;
  3054.  
  3055.   case '<':
  3056.     cur_moveto(list, 0);
  3057.     break;
  3058.  
  3059.   case '>':
  3060.     cur_moveto(list, list->num - 1);
  3061.     break;
  3062.  
  3063.   case K_CTRL + 'L':
  3064.     touchwin (stdscr);
  3065.     refresh();
  3066.     break;
  3067.  
  3068.   default:
  3069.     return 0;
  3070.  
  3071.   }
  3072.  
  3073.   return 1;
  3074. }
  3075.  
  3076. int cur_choice(help, list)
  3077.   char **help;
  3078.   LIST *list;
  3079. {
  3080.   char *cmd = "Select using cursor keys, press RETURN to accept or q to cancel.";
  3081.   int c, ret = 0;
  3082.  
  3083.   list->top = 3 + cur_helpblock(help);
  3084.  
  3085.   cur_displist(list);
  3086.  
  3087.   move(Y_COMMAND, list->type ? (Width - strlen(cmd)) / 2 : 0);
  3088.   addstr(cmd);
  3089.  
  3090.   refresh();
  3091.  
  3092.   for (;;) {
  3093.     c = inp_getchr();
  3094.     if (c == 'q')
  3095.       return -1;
  3096.     if (c == 10 || c == 13) {
  3097.       ret = list->current;
  3098.       goto done;
  3099.     }
  3100.     cur_listnav(list, c);
  3101.     move(Y_COMMAND, strlen(cmd) + (list->type ? (Width - strlen(cmd)) / 2 : 0));
  3102.     refresh();
  3103.   }
  3104.  
  3105. done:
  3106.   return ret;
  3107. }
  3108.  
  3109. int cur_cmdstr(string)
  3110.   char *string;
  3111. {
  3112.   move(Y_COMMAND, X_COMMAND);
  3113.   addstr(string);
  3114.   refresh();
  3115.   OptLen = strlen(string);
  3116.  
  3117.   return 0;
  3118. }
  3119.  
  3120. int cur_subopt(string)
  3121.   char *string;
  3122. {
  3123.   char buf[200], *b = buf;
  3124.   int c, i;
  3125.  
  3126.   cur_cmdstr(string);
  3127.  
  3128.   c = inp_getchr();
  3129.  
  3130.   for (i = 0; string[i] && string[i] != ' '; i++)    /* view   */
  3131.     *b++ = string[i];
  3132.   *b++ = ' ';
  3133.  
  3134.   for (i = strlen(string) - 1; i > 0; i--)    /* a)ll   */
  3135.     if (string[i] == ')' && string[i - 1] == c)
  3136.       break;
  3137.  
  3138.   if (i > 0) {            /* all    */
  3139.     *b++ = string[++i - 2];
  3140.     while (string[i] && string[i] != ' ')
  3141.       *b++ = string[i++];
  3142.   }
  3143.   OptLen = b - buf;
  3144.  
  3145.   while (b < buf + strlen(string))    /* blanks */
  3146.     *b++ = ' ';
  3147.   *b++ = 0;
  3148.  
  3149.   move(Y_COMMAND, X_COMMAND);
  3150.   addstr(buf);
  3151.   move(Y_COMMAND, X_COMMAND + OptLen);
  3152.   refresh();
  3153.   return c;
  3154. }
  3155.  
  3156. char statusbuf[300];
  3157.  
  3158.  
  3159. int cur_statusat(string, y)
  3160.   char *string;
  3161.   int y;
  3162. {
  3163.   int i, sh;
  3164.  
  3165.   for (i = 0; i < COLS - 1; i++)
  3166.     statusbuf[i] = ' ';
  3167.   statusbuf[i] = 0;
  3168.  
  3169.   strcpy(statusbuf + (sh = str_shift(strlen(string), COLS)), string);
  3170.   statusbuf[strlen(statusbuf)] = ' ';
  3171.  
  3172.   move(y, 0);
  3173.   addstr(statusbuf);
  3174.   move(y, sh + strlen(string));
  3175.   refresh();
  3176.  
  3177.   return 0;
  3178. }
  3179.  
  3180. int cur_status(string)
  3181.   char *string;
  3182. {
  3183.   cur_statusat(string, Y_STATUS);
  3184.  
  3185.   return 0;
  3186. }
  3187.  
  3188. int cur_clear()
  3189. {
  3190.   clear();
  3191.   refresh();
  3192.   return 0;
  3193. }
  3194.  
  3195. int cur_report(string)
  3196.   char *string;
  3197. {
  3198.   cur_statusat(string, LINES / 2);
  3199.   return 0;
  3200. }
  3201.  
  3202. int cur_nosubopt()
  3203. {
  3204.   char buf[200], *b = buf;
  3205.  
  3206.   while (OptLen--)
  3207.     *b++ = ' ';
  3208.   *b++ = 0;
  3209.   move(Y_COMMAND, X_COMMAND);
  3210.   addstr(buf);
  3211.   refresh();
  3212.   cur_status("");
  3213.   return 0;
  3214. }
  3215.  
  3216. int cur_more(strings)
  3217.   char **strings;
  3218. {
  3219.   int i, c, lines, offset = 0, len = str_longest(strings);
  3220.   char buf[100], *title = *strings++;
  3221.  
  3222.   for (lines = 0; strings[lines] || lines == 0; lines++);
  3223.  
  3224.   do {
  3225.     clear();
  3226.  
  3227.     move(0, str_shift(strlen(title), len));
  3228.     standout();
  3229.     addstr(title);
  3230.     standend();
  3231.  
  3232.     for (i = 0; i < LINES - 4 && offset + i < lines; i++) {
  3233.       move(2 + i, 0);
  3234.       addstr(strings[offset + i]);
  3235.     }
  3236.  
  3237.     if (offset + i < lines)
  3238.       sprintf(buf, " --- More (%d%%), b)ack, q)uit --- ",
  3239.           (offset + i) * 100 / lines);
  3240.     else
  3241.       strcpy(buf, " --- End --- ");
  3242.  
  3243.     move(MYMIN(LINES - 1, 3 + offset + i), str_shift(strlen(buf), len));
  3244.     standout();
  3245.     addstr(buf);
  3246.     standend();
  3247.     refresh();
  3248.  
  3249.     c = inp_getchr();
  3250.     if (c == 27 || c == 'q')
  3251.       break;
  3252.     else if (c == 'b' || c == K_CTRL + 'H')
  3253.       offset = MYMAX(0, offset - (LINES - 4));
  3254.     else
  3255.       offset += LINES - 4;
  3256.  
  3257.   } while (offset < lines);
  3258.  
  3259.   return 0;
  3260. }
  3261.  
  3262.  
  3263. char *MainHelp[] = {
  3264.   "Aminet Download Tool Help",
  3265.   "Welcome to the Aminet Download Tool. You can select, inspect and download",
  3266.   "files here. The command keys available:",
  3267.   "",
  3268.   "cursor    navigate on list",
  3269.   "blank     next page (also cursor right)",
  3270.   "backspace previous page (also cursor left)",
  3271.   "tab       move down five",
  3272.   "<  >      start and end of list",
  3273.   "",
  3274.   "d)ownload receive selected file(s) after setting options",
  3275.   "f)ind     locate a file anywhere on Aminet",
  3276.   "h)elp     this page",
  3277.   "o)ptions  setup page",
  3278.   "p)rint    create a listing on disk or printer",
  3279.   "q)uit     leave, remembering this call",
  3280.   "Q)uit     leave, without remembering this call",
  3281.   "r)eadme   display the .readme of current file",
  3282.   "s)ort     change sorting of displayed list",
  3283.   "t)ag      mark/unmark current file for later download (also RETURN)",
  3284.   "T)ag      mark/unmark all files",
  3285.   "v)iew     select which files to show",
  3286.   "",
  3287.   "Press ? at any submenu to get help about the commands there.",
  3288.   0
  3289. };
  3290.  
  3291. char *ViewHelp[] = {
  3292.   "The v)iew command",
  3293.   "This command selects which files to display in ATD. The suboptions:",
  3294.   "",
  3295.   "a)ll      shows the complete list of all files on Aminet, downloading it",
  3296.   "          first if necessary. No files are hidden.",
  3297.   "d)ir      pick from the currently known files the ones from the directory",
  3298.   "          you specify.",
  3299.   "h)ide     selects one or more directory (separated by commas) that should",
  3300.   "          not be shown. This info is stored in .adtrc on exit",
  3301.   "k)nown    shows all currently known files, including those that are normally",
  3302.   "          hidden",
  3303.   "l)imit    only show the files which have the given string somewhere in their",
  3304.   "          description or file name",
  3305.   "m)arked   only shows the files that have previously been marked using the",
  3306.   "          t)ag command",
  3307.   "n)ew      shows the new files since your previous call minus the hidden ones,",
  3308.   "          downloading the list if necessary",
  3309.   "s)ite     shows the list of files available at the site you are currently",
  3310.   "          connected to",
  3311.   "t)oggle   toggle between the current list and the previously displayed one.",
  3312.   0
  3313. };
  3314.  
  3315. char *SortHelp[] = {
  3316.   "The s)ort command",
  3317.   "Using this command you can select what order the files should be shown by",
  3318.   "",
  3319.   "a)ge      sorts by age, showing the newest files on top",
  3320.   "A)ge      like a)ge but showing the oldest files on top",
  3321.   "d)ir      sorts alphabetically by directory, subsorting by name",
  3322.   "D)ir      like d)ir but reverse order",
  3323.   "n)ame     sorts alphabetically by file name",
  3324.   "N)ame     like n)ame but reverse order",
  3325.   "s)ize     sorts by file size, largest on top",
  3326.   "S)ize     like s)ize, but smallst on top",
  3327.   0
  3328. };
  3329.  
  3330. int cur_editvar(varname, string)
  3331.   char *varname, *string;
  3332. {
  3333.   char buf[100];
  3334.  
  3335.   cur_status(string);
  3336.   mystrncpy(buf, option_get(varname), 99);
  3337.   cur_getstring(buf);
  3338.   option_set(varname, buf);
  3339.   return 0;
  3340. }
  3341.  
  3342. int cur_showfile(list, i)
  3343.   LIST *list;
  3344.   int i;
  3345. {
  3346.   cur_moveto (list, i);
  3347.   cur_mark   (list, 0);
  3348.   cur_mark   (list, 1);
  3349.  
  3350.   return 0;
  3351. }
  3352.  
  3353. char *DownloadHelp[] = {
  3354.   "The d)ownload command",
  3355.   "This command lets you receive files from Aminet. The suboptions:",
  3356.   "",
  3357.   "a)ction   select what to do after the download. Possibilities are either",
  3358.   "          nothing, transfer using zmodem, or archive unpacking",
  3359.   "b)egin    actually perform the download, using the current settings",
  3360.   "          selected by the other keys",
  3361.   "p)ath     lets you enter the destination path for the files, will be",
  3362.   "          created if it does not exist",
  3363.   "q)uit     do not start download, leave all undownloaded files tagged,",
  3364.   "          and return to main files selection page",
  3365.   "r)eadme   toggle between downloading the .readme files along with the",
  3366.   "          archives and not doing so",
  3367.   "s)ubdirs  toggle between putting all files in the same directory vs",
  3368.   "          re-creating the Aminet directory structure",
  3369.   "v)erbose  toggle between quiet and verbose download, where quiet suspends",
  3370.   "          all screen output until all files have been downloaded.",
  3371.   "",
  3372.   "The current settings are displayed in the bottom line. If you want to",
  3373.   "change those settings permanently, use o)ptions d)ownload.",
  3374.   0
  3375. };
  3376.  
  3377. int cur_download()
  3378. {
  3379.   LIST *l;
  3380.   int cont = 1;
  3381.   char path[300];
  3382.  
  3383.   markedextract();
  3384.  
  3385.   l = Vis;
  3386.   Vis = Mark;
  3387.   Mark = l;
  3388.  
  3389.   Flat = option_getnum("flatdl");
  3390.   Readme = option_getnum("readmedl");
  3391.   Silent = option_getnum("silentdl");
  3392.   strcpy(Path, option_get("dlpath"));
  3393.  
  3394.   cur_dispmask();
  3395.  
  3396.   do {
  3397.     cur_status(transferstr());
  3398.     cur_cmdstr("");
  3399.  
  3400.     switch (inp_getchr()) {
  3401.     case '?':
  3402.     case 'h':
  3403.       cur_more(DownloadHelp);
  3404.       cur_dispmask();
  3405.       break;
  3406.  
  3407.     case 'p':
  3408.       cur_cmdstr("path");
  3409.       cur_editvar("dlpath", "Enter path to download files to");
  3410.       strcpy(Path, option_get("dlpath"));
  3411.       strcpy(path, Path);
  3412.       make_dirs (glob_path (path));
  3413.       cur_nosubopt();
  3414.       break;
  3415.  
  3416.     case 'b':
  3417.       cur_moveto(Vis, 0);
  3418.       get_files(cur_status, cur_showfile);
  3419.       cur_nosubopt();
  3420.       cont = 0;
  3421.       break;
  3422.  
  3423.     case 'a':
  3424.       Action = ++Action % 3;
  3425.       break;
  3426.     case 'q':
  3427.       cont = 0;
  3428.       break;
  3429.     case 'r':
  3430.       Readme = !Readme;
  3431.       break;
  3432.     case 's':
  3433.       Flat = !Flat;
  3434.       break;
  3435.     case 'v':
  3436.       Silent = !Silent;
  3437.       break;
  3438.     }
  3439.  
  3440.   } while (cont);
  3441.  
  3442.   l = Vis;
  3443.   Vis = Mark;
  3444.   Mark = l;
  3445.  
  3446.   return 0;
  3447. }
  3448.  
  3449. char *OptionsHelp[] = {
  3450.   "The o)ptions command",
  3451.   "Using the o)ptions command you can modify various settings of ADT. They",
  3452.   "will be saved in .adtrc when you leave ADT using the q)uit command, but",
  3453.   "not if you use Q)uit. The settings you can modify are:",
  3454.   "",
  3455.   "c)ompression lets you pick the compression type used for downloading",
  3456.   "             Aminet contents files.",
  3457.   "d)ownload    will pick the defaults for downloading readmes, verbose or",
  3458.   "             quiet download, download path and so on",
  3459.   "f)ind        lets you pick the method used for finding things on Aminet,",
  3460.   "             ie. download and local search vs. using a search server",
  3461.   "m)ethod      will choose the method used to transfer files, e.g. FTP",
  3462.   "             mail server or local files",
  3463.   "p)rint       lets you choose the format of the listings generated by ADT",
  3464.   "             mostly wide vs. narrow format",
  3465.   "s)ite        this lets you pick the location you want to get your files",
  3466.   "             from, for example which FTP site to use", 0
  3467. };
  3468.  
  3469.  
  3470. char *CompressOptHelp[] = {
  3471.   "Compression type",
  3472.   "Please select the compression method to use for transmitting the Aminet",
  3473.   "index files. If you have very fast access (like local files), pick no",
  3474.   "compression, otherwise pick compress, unless you have 'gzip' in your",
  3475.   "path, which compresses even better at the same speed. Note, we're only",
  3476.   "talking about decompression here, its only the choice between different",
  3477.   "versions of the index files in fact.", 0
  3478. }, *CompressList[] = {
  3479.   "  none  ",
  3480.   "compress",
  3481.   "  gzip  ", 0
  3482. };
  3483.  
  3484. char *FlatOptHelp[] = {
  3485.   "Flat or tree download",
  3486.   "Please choose whether to put all downloaded files in the same",
  3487.   "directory or to create the correct subdirectory as on Aminet ",
  3488.   "for each file downloaded", 0
  3489. }, *FlatDlList[] = {
  3490.   "subdir (create subdirs)",
  3491.   "flat   (same directory)", 0
  3492. };
  3493.  
  3494. char *ReadmeOptHelp[] = {
  3495.   "Readme download",
  3496.   "Please choose whether or not to download the .readme files",
  3497.   "along with the archives", 0
  3498. }, *ReadmeDlList[] = {
  3499.   "download archives only",
  3500.   "download .readme files too", 0
  3501. };
  3502.  
  3503. char *QuietOptHelp[] = {
  3504.   "Silent download",
  3505.   "Please choose wheter or not to perform transfers quietly. If you turn on",
  3506.   "quiet mode, no screen output happens until the download is completey over.",
  3507.   "This allows you to interrupt ADT using CTRL-Z and put it in the background",
  3508.   "using 'bg' to download quietly in the background", 0
  3509. }, *QuietDlList[] = {
  3510.   "give progress reports",
  3511.   "download quietly", 0
  3512. };
  3513.  
  3514. char *FindSitesHelp[] = {
  3515.   "Find site",
  3516.   "Here you can pick the find server to use for your queries. Feel free to",
  3517.   "them all and choose the fastest. All find servers have the complete list",
  3518.   "of all Aminet files online", 0
  3519. };
  3520.  
  3521. char FindString[120];
  3522.  
  3523. int cur_cmdkeys(c)
  3524.   int c;
  3525. {
  3526.   char buf[200];
  3527.   SORT *sorttype;
  3528.   int noredraw = 0, width = 0;
  3529.  
  3530.   buf[0] = 0;
  3531.  
  3532.   switch (c) {
  3533.  
  3534.   case 'd':
  3535.     if (Vis->num)
  3536.       cur_download();
  3537.     break;
  3538.  
  3539.   case 'f':
  3540.     cur_cmdstr("find");
  3541.     cur_status("(Use 'adt -f <keyword>' to start finds from the command line)");
  3542.     FindString[0] = 0;
  3543.     cur_getstring(FindString);
  3544.     if (*FindString)
  3545.       find_string(FindString, cur_status);
  3546.     break;
  3547.  
  3548.   case 'o':
  3549. options:
  3550.     switch (cur_subopt("options c)ompression d)ownload f)ind m)ethod s)ite")) {
  3551.     case '?':
  3552.     case 'h':
  3553.       cur_more(OptionsHelp);
  3554.       cur_dispmask();
  3555.       goto options;
  3556.  
  3557.     case 'c':
  3558.       setup_enumed(CompressOptHelp, CompressList, "compress");
  3559.       break;
  3560.  
  3561.     case 'd':
  3562.       cur_editvar("dlpath", "Enter path to download files to"),
  3563.       cur_editvar("send", "Enter command to use for sending files, eg. 'sz'");
  3564.       setup_enumed(FlatOptHelp, FlatDlList, "flatdl");
  3565.       setup_enumed(ReadmeOptHelp, ReadmeDlList, "readmedl");
  3566.       setup_enumed(QuietOptHelp, QuietDlList, "quietdl");
  3567.       break;
  3568.  
  3569.     case 'f':
  3570.       setup_findmethod();
  3571.       break;
  3572.  
  3573.     case 'm':
  3574.       trans_close();
  3575.       e_freeall();
  3576.       setup_method();
  3577.       get_recent();
  3578.       newvisible();
  3579.       NoFilesString = "Use v)iew or f)ind to get files to the display";
  3580.       break;
  3581.  
  3582.     case 's':
  3583.       if (trans_options())
  3584.         break;
  3585.       if (CdRom) {
  3586.         get_sitelocal();
  3587.         allvisible();
  3588.       } else {
  3589.         get_recent();
  3590.         newvisible();
  3591.       }
  3592.       break;
  3593.  
  3594.     default:
  3595.       noredraw = 1;
  3596.     }
  3597.     cur_nosubopt();
  3598.  
  3599.     break;
  3600.  
  3601.   case 'p':
  3602.     cur_status("Narrow format fits 80 columns, wide format format 132 columns");
  3603.     switch (cur_subopt("print n)arrow w)ide")) {
  3604.     case 'n':
  3605.       width = 1;
  3606.       break;
  3607.     case 'w':
  3608.       width = 2;
  3609.       break;
  3610.     }
  3611.     if (width) {
  3612.       cur_editvar("printer", "Enter the file to print the visible files to");
  3613.       print_listing(width);
  3614.     }
  3615.     cur_nosubopt();
  3616.     noredraw = 1;
  3617.     break;
  3618.  
  3619.   case 'q':
  3620.     cur_cmdstr("quit (and save config file)");
  3621.     option_save(ConfigFile);
  3622.     exit_adt(0);
  3623.     break;
  3624.  
  3625.   case 'Q':
  3626.     cur_cmdstr("quit (without saving config file)");
  3627.     exit_adt(0);
  3628.     break;
  3629.  
  3630.   case 'v':
  3631. view:
  3632.     switch (cur_subopt("view a)ll d)ir h)ide k)nown l)imit m)arked n)ew s)ite t)oggle")) {
  3633.     case '?':
  3634.       cur_more(ViewHelp);
  3635.       cur_dispmask();
  3636.       goto view;
  3637.  
  3638.     case 'a':
  3639.       if (!GotComplete) {
  3640.     get_complete();
  3641.     allvisible();
  3642.       }
  3643.       break;
  3644.  
  3645.     case 'd':
  3646.       cur_status("Select directory to show exclusively");
  3647.       dirvisible(cur_getstring(buf));
  3648.       cur_status("");
  3649.       break;
  3650.  
  3651.     case 'h':
  3652.       cur_editvar("hide", "Enter directories to hide permanently");
  3653.       invisible();
  3654.       break;
  3655.  
  3656.     case 'k':
  3657.       allvisible();
  3658.       break;
  3659.  
  3660.     case 'l':
  3661.       cur_status("Enter string to match in currently displayed descriptions");
  3662.       strlimit(cur_getstring(buf));
  3663.       cur_status("");
  3664.       break;
  3665.  
  3666.     case 'm':
  3667.       markedvisible();
  3668.       break;
  3669.  
  3670.     case 'n':
  3671.       get_recent();
  3672.       newvisible();
  3673.       break;
  3674.  
  3675.     case 's':
  3676.       if (!GotLocal) {
  3677.     get_sitelocal();
  3678.     cur_clear();
  3679.     cur_report("Sorting...");
  3680.     allvisible();
  3681.       }
  3682.       break;
  3683.  
  3684.     case 't':
  3685.       togglevisible();
  3686.       break;
  3687.  
  3688.     default:
  3689.       noredraw = 1;
  3690.     }
  3691.  
  3692.     cur_nosubopt();
  3693.     break;
  3694.  
  3695.   case 'r':
  3696.     if (Vis->num) {
  3697.       ENTRY *e=Vis->list[Vis->current];
  3698.  
  3699.       strcpy(buf, e->dir);
  3700.       tackon(buf, get_readme(e));
  3701.       adt_connect (disp_status, e->status & 1);
  3702.       display_remote(buf, get_readme(e));
  3703.       adt_disconnect();
  3704.     }
  3705.     break;
  3706.  
  3707.   case 's':
  3708.     sorttype = 0;
  3709. sort:
  3710.     switch (cur_subopt("sort a)ge d)ir n)ame s)ize")) {
  3711.     case '?':
  3712.       cur_more(SortHelp);
  3713.       cur_dispmask();
  3714.       goto sort;
  3715.     case 'a':
  3716.       sorttype = &sort_age;
  3717.       break;
  3718.     case 'd':
  3719.       sorttype = &sort_dir;
  3720.       break;
  3721.     case 'n':
  3722.       sorttype = &sort_name;
  3723.       break;
  3724.     case 's':
  3725.       sorttype = &sort_size;
  3726.       break;
  3727.     case 'A':
  3728.       sorttype = &sort_rage;
  3729.       break;
  3730.     case 'D':
  3731.       sorttype = &sort_rdir;
  3732.       break;
  3733.     case 'N':
  3734.       sorttype = &sort_rname;
  3735.       break;
  3736.     case 'S':
  3737.       sorttype = &sort_rsize;
  3738.       break;
  3739.     }
  3740.     if (sorttype) {
  3741.       cur_status("Sorting...");
  3742.       sort_list(Vis, sorttype);
  3743.       cur_dispfiles(Vis);
  3744.       cur_status("");
  3745.     }
  3746.     noredraw = 1;
  3747.     cur_nosubopt();
  3748.     break;
  3749.  
  3750.   case 'h':
  3751.   case '?':
  3752.     cur_more(MainHelp);
  3753.     break;
  3754.  
  3755.   case 't':
  3756.   case 13: case 10:
  3757.     noredraw = 1;
  3758.     if (!Vis->num)
  3759.       break;
  3760.     cur_mark(Vis, 0);
  3761.     tagcurrent();
  3762.     cur_mark(Vis, 1);
  3763.     cur_moveto(Vis, Vis->current + 1);
  3764.     break;
  3765.  
  3766.   case 'T':
  3767.     tagall();
  3768.     break;
  3769.  
  3770.   default:
  3771.     noredraw = 1;
  3772.  
  3773.   }
  3774.  
  3775.   if (!noredraw)
  3776.     cur_dispmask();
  3777.  
  3778.   return 0;
  3779. }
  3780.  
  3781.  
  3782.  
  3783. int cur_mainloop()
  3784. {
  3785.   int c;
  3786.  
  3787.   refresh();
  3788.   cur_dispmask();
  3789.   refresh();
  3790.  
  3791.   while ((c = inp_getchr())) {
  3792.     cur_listnav(Vis, c);
  3793.     cur_cmdkeys(c);
  3794.     move(Y_COMMAND, X_COMMAND);
  3795.     refresh();
  3796.   }
  3797.  
  3798.   return 0;
  3799. }
  3800.  
  3801. int cur_cleanup()
  3802. {
  3803.   nocbreak();
  3804.   echo();
  3805.   endwin();
  3806. #ifndef AMIGA
  3807.   puts("");
  3808. #endif
  3809.   return 0;
  3810. }
  3811.  
  3812. int cur_getcmd()
  3813. {
  3814.   return getchr();
  3815. }
  3816.  
  3817.  
  3818. int cur_confirm(strings, retry)
  3819.   char **strings;
  3820.   int retry;
  3821. {
  3822.   int i, j, m = str_shift(str_longest(strings), COLS);
  3823.  
  3824.   for (i = 0; strings[i]; i++);
  3825.  
  3826.   clear();
  3827.  
  3828.   for (j = 0; j < i; j++) {
  3829.     move((LINES - i - 2) / 2 + j, m);
  3830.     addstr(strings[j]);
  3831.   }
  3832.  
  3833.   move((LINES - i - 2) / 2 + j + 1, m + str_longest(strings) - strlen("Press any key"));
  3834.   addstr("Press any key");
  3835.  
  3836.   refresh();
  3837.   inp_getchr();
  3838.  
  3839.   return 0;
  3840. }
  3841.  
  3842. int cur_refresh()
  3843. {
  3844.   refresh();
  3845.  
  3846.   return 0;
  3847. }
  3848.  
  3849. #endif
  3850.  
  3851. /*---------------------------------main program------------------------------*/
  3852. int init_vars()
  3853. {
  3854.   char *d, *t;
  3855.   int i;
  3856.  
  3857.   mystrcpy(HomeDir, (t = (char *) getenv("HOME")) ? t : "");
  3858.  
  3859.   if (*HomeDir) {
  3860.     d = UserName;
  3861.     for (i = 0; HomeDir[i]; i++)
  3862.       if (HomeDir[i] == '/')
  3863.     d = UserName;
  3864.       else
  3865.     *d++ = HomeDir[i];
  3866.     *d++ = 0;
  3867.   }
  3868. #ifdef AMIGA
  3869.   mystrcpy(ConfigFile, "S:.adtrc");
  3870. #else
  3871.   mystrcpy(ConfigFile, HomeDir);
  3872.   tackon(ConfigFile, ".adtrc");
  3873. #endif
  3874.  
  3875.   Alt->sort = Vis->sel = "";
  3876.   Alt->sort = Vis->sort = "";
  3877.   Alt->cmds = Vis->cmds = "d)ownload f)ind h)elp o)ptions p)rint q)uit r)eadme s)ort t)ag v)iew";
  3878.  
  3879.   Mark->sel = "Files to be downloaded";
  3880.   Mark->sort = "";
  3881.   Mark->cmds = "a)ction b)egin p)ath q)uit r)eadmes s)ubdirs v)erbose";
  3882.  
  3883.   return 0;
  3884. }
  3885.  
  3886. char recentbuf[100];
  3887. char *FirstConnectHelp[] = {
  3888.   "This seems to be the first time you connect. You'll be seeing the last",
  3889.   "14 days' uploads now. Next time you connect, all the files that were",
  3890.   "uploaded since this connection will be shown.", 0
  3891. };
  3892.  
  3893. char *LongAgoHelp[] = {
  3894.   "Your last connection was more than 14 days ago, but you'll only be seeing",
  3895.   "the last 14 days' uploads.  If you want to see everything, use v)iew a)ll",
  3896.   "and filter accordingly.", 0
  3897. };
  3898.  
  3899. int get_list(title, name)
  3900.   char *title, *name;
  3901. {
  3902.   TEMPFILE tfh;
  3903.   char buf[250];
  3904.   char *packer = "";
  3905.   long amotd = 0, lmotd = 0, sites = 0;
  3906.  
  3907.   e_freeall();
  3908.  
  3909.   if (option_getnum("compress") == 1)
  3910.     packer = ".Z";
  3911.  
  3912.   if (option_getnum("compress") == 2)
  3913.     packer = ".gz";
  3914.  
  3915.   sprintf(buf, "info/adt/%s", name);
  3916.  
  3917.   disp_clear();
  3918.   disp_report(title);
  3919.  
  3920.   adt_connect(disp_report, STATUS_LOCAL);
  3921.  
  3922.   NoFilesString = "Could not download index file";
  3923.   trans_remopen(&tfh, buf, packer, disp_report);
  3924.  
  3925.   if (tfh.fh) {
  3926.  
  3927.     disp_report("Reading index");
  3928.     do {
  3929.       *parsebuf = 0;
  3930.       fgets(parsebuf, PBUFSIZE, tfh.fh);
  3931.       if (!mystrncmp(parsebuf, "#amotd=", 7))
  3932.     amotd = p_atoi(parsebuf + 7);
  3933.       if (!mystrncmp(parsebuf, "#lmotd=", 7))
  3934.     lmotd = p_atoi(parsebuf + 7);
  3935.       if (!mystrncmp(parsebuf, "#sites=", 7))
  3936.     sites = p_atoi(parsebuf + 7);
  3937.  
  3938.     } while (*parsebuf == '#');
  3939.  
  3940.     read_adt_v2(tfh.fh);
  3941.     tclose(&tfh);
  3942.  
  3943.   }
  3944.  
  3945.   if (!option_getnum("nomotd")) {
  3946.     if (amotd > option_getnum("amotd")) {
  3947.       disp_clear();
  3948.       disp_report("Retrieving new Aminet message of the day");
  3949.       display_remote("info/adt/aminet-motd","Aminet message of the day");
  3950.       option_setnum("amotd", amotd);
  3951.     }
  3952.     if (lmotd > option_getnum("lmotd")) {
  3953.       disp_clear();
  3954.       disp_report("Retrieving new local message of the day");
  3955.       display_remote("info/adt/local-motd","Mirror message of the day");
  3956.       option_setnum("lmotd", lmotd);
  3957.     }
  3958.   }
  3959.   if (sites > option_getnum("sites")) {
  3960.     disp_clear();
  3961.     disp_report("Updating mirror list");
  3962.     trans_remopen(&tfh, "info/adt/sites", "", disp_status);
  3963.     if (tfh.fh) {
  3964.       option_loadfh(tfh.fh);
  3965.       tclose(&tfh);
  3966.     }
  3967.     option_setnum("sites", sites);
  3968.   }
  3969.  
  3970.   adt_disconnect();
  3971.  
  3972.   return 0;
  3973. }
  3974.  
  3975. int get_complete()
  3976. {
  3977.   if (!GotComplete)
  3978.     get_list("Downloading the list of all Aminet files", "ADT_AMINET");
  3979.  
  3980.   GotComplete = GotLocal = GotRecent = 1;
  3981.  
  3982.   return 0;
  3983. }
  3984.  
  3985. int get_sitelocal()
  3986. {
  3987.   if (!GotLocal)
  3988.     get_list("Downloading the list of files on this site", "ADT_LOCAL");
  3989.  
  3990.   GotLocal = GotRecent = 1;
  3991.  
  3992.   return 0;
  3993. }
  3994.  
  3995.  
  3996. int get_recent()
  3997. {
  3998.   long age;
  3999.  
  4000.   if (GotRecent)
  4001.     return 0;
  4002.  
  4003.   GotRecent = 1;
  4004.  
  4005.   LastCall = myatoi(option_get("newest"));
  4006.   age = time(NULL) - LastCall;
  4007.  
  4008.   if (LastCall == 0) {
  4009.     disp_confirm(FirstConnectHelp, 0);
  4010.     get_list("Downloading the list of new files", "ADT_RECENT_14");
  4011.   } else if (age < 7 * 86400) {
  4012.     get_list("Downloading the list of new files", "ADT_RECENT_7");
  4013.   } else if (age < 14 * 86400) {
  4014.     get_list("Downloading the list of new files", "ADT_RECENT_14");
  4015.   } else {
  4016.     disp_confirm(LongAgoHelp, 0);
  4017.     get_list("Downloading the list of new files", "ADT_RECENT_14");
  4018.   }
  4019.  
  4020.   if (newesttime() > option_getnum("newest"))
  4021.     option_setnum("newest", newesttime());
  4022.  
  4023.   return 0;
  4024. }
  4025.  
  4026. int init_display()
  4027. {
  4028.   disp_init = cur_dispinit;
  4029.   disp_getchar = cur_getchar;
  4030.   disp_cleanup = cur_cleanup;
  4031.   disp_choice = cur_choice;
  4032.   disp_mainloop = cur_mainloop;
  4033.   disp_report = cur_report;
  4034.   disp_status = cur_status;
  4035.   disp_confirm = cur_confirm;
  4036.   disp_more = cur_more;
  4037.   disp_inputstr = cur_inputstr;
  4038.   disp_clear = cur_clear;
  4039.   disp_refresh = cur_refresh;
  4040.  
  4041.   disp_init();
  4042.  
  4043. #ifdef SIGINT
  4044.   signal (SIGINT, adt_breakcheck);
  4045. #endif
  4046.  
  4047.   return 0;
  4048. }
  4049.  
  4050.  
  4051. int init_config()
  4052. {
  4053.   if (option_load(ConfigFile) || trans_init()) {
  4054.  
  4055.     InitialSetup = 1;            /* was 1 originally, but must be 0 to use predefined options */
  4056.     setup_method();
  4057.     InitialSetup = 0;
  4058.     option_save(ConfigFile);
  4059.  
  4060.   }
  4061.   return 0;
  4062. }
  4063.  
  4064. int show_usage()
  4065. {
  4066.   puts("Usage: adt               (show new files since last call)");
  4067.   puts("       adt -f pattern    (find pattern using configured method)");
  4068.   puts("       adt -l            (show files local to configured site)");
  4069.   puts("       adt -n            (don't connect)");
  4070.   exit_adt(0);
  4071.   return 0;
  4072. }
  4073.  
  4074. int parse_commandline(argc, argv)
  4075.   int argc;
  4076.   char *argv[];
  4077. {
  4078.   if (argc <= 1) {
  4079.     if (CdRom) {
  4080.       get_sitelocal();
  4081.       allvisible();
  4082.     } else {
  4083.       get_recent();
  4084.       newvisible();
  4085.     }
  4086.     return 0;
  4087.   }
  4088.   if (argv[1][0] != '-' || !argv[1][1] || argv[1][2])
  4089.     show_usage();
  4090.  
  4091.   switch (argv[1][1]) {
  4092.   case 'f':
  4093.     if (argc != 3)
  4094.       show_usage();
  4095.     find_string(argv[2], cur_report);
  4096.     break;
  4097.  
  4098.   case 'l':
  4099.     if (argc != 2)
  4100.       show_usage();
  4101.     get_sitelocal();
  4102.     newvisible();
  4103.     break;
  4104.  
  4105.   case 'n':
  4106.     if (argc != 2)
  4107.       show_usage();
  4108.     NoFilesString="Use o)ptions to connect to a service/site";
  4109.     break;
  4110.  
  4111.   default:
  4112.     show_usage();
  4113.   }
  4114.  
  4115.   return 0;
  4116. }
  4117.  
  4118.  
  4119. int main(argc, argv)
  4120.   int argc;
  4121.   char *argv[];
  4122. {
  4123.   init_vars();
  4124.   init_display();
  4125.   init_config();
  4126.  
  4127.   parse_commandline(argc, argv);
  4128.   disp_mainloop();
  4129.  
  4130.   exit_adt(0);
  4131.   return 0;
  4132. }
  4133.  
  4134. TRANSFER TransferTypes[] =
  4135. {
  4136. #ifndef NO_BUILTIN_FTP
  4137.   "bftp",   "Builtin FTP: Use internal ftp routines to download files     ", bftp_init,
  4138. #endif
  4139. #ifndef NO_EXTERNAL_FTP
  4140.   "ftp",   "External FTP: Use the 'ftp' program to download files         ", xftp_init,
  4141. #endif
  4142.   "local", "Local files : Access a local, updated Aminet file collection  ", local_init,
  4143.   "cdrom", "CD-ROM      : Access an Aminet CD-ROM or non-updated files    ", local_init,
  4144.   0, 0, 0
  4145. };
  4146.  
  4147.  
  4148. /* to delete: ftp.log, pad, recent, short */
  4149.  
  4150.